aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
Diffstat (limited to 'sound')
-rw-r--r--sound/oss/Makefile9
-rw-r--r--sound/oss/ac97_codec.c284
-rw-r--r--sound/oss/btaudio.c1139
-rw-r--r--sound/oss/cs4232.c526
-rw-r--r--sound/oss/i810_audio.c3656
-rw-r--r--sound/oss/via82cxxx_audio.c3616
6 files changed, 0 insertions, 9230 deletions
diff --git a/sound/oss/Makefile b/sound/oss/Makefile
index f883c4b676ab..1f86299fae40 100644
--- a/sound/oss/Makefile
+++ b/sound/oss/Makefile
@@ -6,7 +6,6 @@
6# Each configuration option enables a list of files. 6# Each configuration option enables a list of files.
7 7
8obj-$(CONFIG_SOUND_OSS) += sound.o 8obj-$(CONFIG_SOUND_OSS) += sound.o
9obj-$(CONFIG_SOUND_CS4232) += cs4232.o ad1848.o
10 9
11# Please leave it as is, cause the link order is significant ! 10# Please leave it as is, cause the link order is significant !
12 11
@@ -16,7 +15,6 @@ obj-$(CONFIG_SOUND_AEDSP16) += aedsp16.o
16obj-$(CONFIG_SOUND_PSS) += pss.o ad1848.o mpu401.o 15obj-$(CONFIG_SOUND_PSS) += pss.o ad1848.o mpu401.o
17obj-$(CONFIG_SOUND_TRIX) += trix.o ad1848.o sb_lib.o uart401.o 16obj-$(CONFIG_SOUND_TRIX) += trix.o ad1848.o sb_lib.o uart401.o
18obj-$(CONFIG_SOUND_SSCAPE) += sscape.o ad1848.o mpu401.o 17obj-$(CONFIG_SOUND_SSCAPE) += sscape.o ad1848.o mpu401.o
19obj-$(CONFIG_SOUND_CS4232) += cs4232.o uart401.o
20obj-$(CONFIG_SOUND_MSS) += ad1848.o 18obj-$(CONFIG_SOUND_MSS) += ad1848.o
21obj-$(CONFIG_SOUND_PAS) += pas2.o sb.o sb_lib.o uart401.o 19obj-$(CONFIG_SOUND_PAS) += pas2.o sb.o sb_lib.o uart401.o
22obj-$(CONFIG_SOUND_SB) += sb.o sb_lib.o uart401.o 20obj-$(CONFIG_SOUND_SB) += sb.o sb_lib.o uart401.o
@@ -27,19 +25,12 @@ obj-$(CONFIG_SOUND_YM3812) += opl3.o
27obj-$(CONFIG_SOUND_VMIDI) += v_midi.o 25obj-$(CONFIG_SOUND_VMIDI) += v_midi.o
28obj-$(CONFIG_SOUND_VIDC) += vidc_mod.o 26obj-$(CONFIG_SOUND_VIDC) += vidc_mod.o
29obj-$(CONFIG_SOUND_WAVEARTIST) += waveartist.o 27obj-$(CONFIG_SOUND_WAVEARTIST) += waveartist.o
30
31obj-$(CONFIG_SOUND_VIA82CXXX) += via82cxxx_audio.o ac97_codec.o
32ifeq ($(CONFIG_MIDI_VIA82CXXX),y)
33 obj-$(CONFIG_SOUND_VIA82CXXX) += sound.o uart401.o
34endif
35obj-$(CONFIG_SOUND_MSNDCLAS) += msnd.o msnd_classic.o 28obj-$(CONFIG_SOUND_MSNDCLAS) += msnd.o msnd_classic.o
36obj-$(CONFIG_SOUND_MSNDPIN) += msnd.o msnd_pinnacle.o 29obj-$(CONFIG_SOUND_MSNDPIN) += msnd.o msnd_pinnacle.o
37obj-$(CONFIG_SOUND_VWSND) += vwsnd.o 30obj-$(CONFIG_SOUND_VWSND) += vwsnd.o
38obj-$(CONFIG_SOUND_ICH) += i810_audio.o ac97_codec.o
39obj-$(CONFIG_SOUND_AU1550_AC97) += au1550_ac97.o ac97_codec.o 31obj-$(CONFIG_SOUND_AU1550_AC97) += au1550_ac97.o ac97_codec.o
40obj-$(CONFIG_SOUND_TRIDENT) += trident.o ac97_codec.o 32obj-$(CONFIG_SOUND_TRIDENT) += trident.o ac97_codec.o
41obj-$(CONFIG_SOUND_BCM_CS4297A) += swarm_cs4297a.o 33obj-$(CONFIG_SOUND_BCM_CS4297A) += swarm_cs4297a.o
42obj-$(CONFIG_SOUND_BT878) += btaudio.o
43 34
44obj-$(CONFIG_SOUND_WM97XX) += ac97_plugin_wm97xx.o 35obj-$(CONFIG_SOUND_WM97XX) += ac97_plugin_wm97xx.o
45 36
diff --git a/sound/oss/ac97_codec.c b/sound/oss/ac97_codec.c
index fef56cac06c8..87a672680761 100644
--- a/sound/oss/ac97_codec.c
+++ b/sound/oss/ac97_codec.c
@@ -189,42 +189,6 @@ static const struct {
189 {0x57454301, "Winbond 83971D", &null_ops}, 189 {0x57454301, "Winbond 83971D", &null_ops},
190}; 190};
191 191
192static const char *ac97_stereo_enhancements[] =
193{
194 /* 0 */ "No 3D Stereo Enhancement",
195 /* 1 */ "Analog Devices Phat Stereo",
196 /* 2 */ "Creative Stereo Enhancement",
197 /* 3 */ "National Semi 3D Stereo Enhancement",
198 /* 4 */ "YAMAHA Ymersion",
199 /* 5 */ "BBE 3D Stereo Enhancement",
200 /* 6 */ "Crystal Semi 3D Stereo Enhancement",
201 /* 7 */ "Qsound QXpander",
202 /* 8 */ "Spatializer 3D Stereo Enhancement",
203 /* 9 */ "SRS 3D Stereo Enhancement",
204 /* 10 */ "Platform Tech 3D Stereo Enhancement",
205 /* 11 */ "AKM 3D Audio",
206 /* 12 */ "Aureal Stereo Enhancement",
207 /* 13 */ "Aztech 3D Enhancement",
208 /* 14 */ "Binaura 3D Audio Enhancement",
209 /* 15 */ "ESS Technology Stereo Enhancement",
210 /* 16 */ "Harman International VMAx",
211 /* 17 */ "Nvidea 3D Stereo Enhancement",
212 /* 18 */ "Philips Incredible Sound",
213 /* 19 */ "Texas Instruments 3D Stereo Enhancement",
214 /* 20 */ "VLSI Technology 3D Stereo Enhancement",
215 /* 21 */ "TriTech 3D Stereo Enhancement",
216 /* 22 */ "Realtek 3D Stereo Enhancement",
217 /* 23 */ "Samsung 3D Stereo Enhancement",
218 /* 24 */ "Wolfson Microelectronics 3D Enhancement",
219 /* 25 */ "Delta Integration 3D Enhancement",
220 /* 26 */ "SigmaTel 3D Enhancement",
221 /* 27 */ "Winbond 3D Stereo Enhancement",
222 /* 28 */ "Rockwell 3D Stereo Enhancement",
223 /* 29 */ "Reserved 29",
224 /* 30 */ "Reserved 30",
225 /* 31 */ "Reserved 31"
226};
227
228/* this table has default mixer values for all OSS mixers. */ 192/* this table has default mixer values for all OSS mixers. */
229static struct mixer_defaults { 193static struct mixer_defaults {
230 int mixer; 194 int mixer;
@@ -614,83 +578,6 @@ static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned
614 return -EINVAL; 578 return -EINVAL;
615} 579}
616 580
617/* entry point for /proc/driver/controller_vendor/ac97/%d */
618int ac97_read_proc (char *page, char **start, off_t off,
619 int count, int *eof, void *data)
620{
621 int len = 0, cap, extid, val, id1, id2;
622 struct ac97_codec *codec;
623 int is_ac97_20 = 0;
624
625 if ((codec = data) == NULL)
626 return -ENODEV;
627
628 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
629 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
630 len += sprintf (page+len, "Vendor name : %s\n", codec->name);
631 len += sprintf (page+len, "Vendor id : %04X %04X\n", id1, id2);
632
633 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
634 extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
635 len += sprintf (page+len, "AC97 Version : %s\n",
636 extid ? "2.0 or later" : "1.0");
637 if (extid) is_ac97_20 = 1;
638
639 cap = codec->codec_read(codec, AC97_RESET);
640 len += sprintf (page+len, "Capabilities :%s%s%s%s%s%s\n",
641 cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
642 cap & 0x0002 ? " -reserved1-" : "",
643 cap & 0x0004 ? " -bass & treble-" : "",
644 cap & 0x0008 ? " -simulated stereo-" : "",
645 cap & 0x0010 ? " -headphone out-" : "",
646 cap & 0x0020 ? " -loudness-" : "");
647 val = cap & 0x00c0;
648 len += sprintf (page+len, "DAC resolutions :%s%s%s\n",
649 " -16-bit-",
650 val & 0x0040 ? " -18-bit-" : "",
651 val & 0x0080 ? " -20-bit-" : "");
652 val = cap & 0x0300;
653 len += sprintf (page+len, "ADC resolutions :%s%s%s\n",
654 " -16-bit-",
655 val & 0x0100 ? " -18-bit-" : "",
656 val & 0x0200 ? " -20-bit-" : "");
657 len += sprintf (page+len, "3D enhancement : %s\n",
658 ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
659
660 val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
661 len += sprintf (page+len, "POP path : %s 3D\n"
662 "Sim. stereo : %s\n"
663 "3D enhancement : %s\n"
664 "Loudness : %s\n"
665 "Mono output : %s\n"
666 "MIC select : %s\n"
667 "ADC/DAC loopback : %s\n",
668 val & 0x8000 ? "post" : "pre",
669 val & 0x4000 ? "on" : "off",
670 val & 0x2000 ? "on" : "off",
671 val & 0x1000 ? "on" : "off",
672 val & 0x0200 ? "MIC" : "MIX",
673 val & 0x0100 ? "MIC2" : "MIC1",
674 val & 0x0080 ? "on" : "off");
675
676 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
677 cap = extid;
678 len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
679 cap & 0x0001 ? " -var rate PCM audio-" : "",
680 cap & 0x0002 ? " -2x PCM audio out-" : "",
681 cap & 0x0008 ? " -var rate MIC in-" : "",
682 cap & 0x0040 ? " -PCM center DAC-" : "",
683 cap & 0x0080 ? " -PCM surround DAC-" : "",
684 cap & 0x0100 ? " -PCM LFE DAC-" : "",
685 cap & 0x0200 ? " -slot/DAC mappings-" : "");
686 if (is_ac97_20) {
687 len += sprintf (page+len, "Front DAC rate : %d\n",
688 codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
689 }
690
691 return len;
692}
693
694/** 581/**
695 * codec_id - Turn id1/id2 into a PnP string 582 * codec_id - Turn id1/id2 into a PnP string
696 * @id1: Vendor ID1 583 * @id1: Vendor ID1
@@ -1313,176 +1200,5 @@ static int pt101_init(struct ac97_codec * codec)
1313#endif 1200#endif
1314 1201
1315 1202
1316EXPORT_SYMBOL(ac97_read_proc);
1317EXPORT_SYMBOL(ac97_probe_codec); 1203EXPORT_SYMBOL(ac97_probe_codec);
1318 1204
1319/*
1320 * AC97 library support routines
1321 */
1322
1323/**
1324 * ac97_set_dac_rate - set codec rate adaption
1325 * @codec: ac97 code
1326 * @rate: rate in hertz
1327 *
1328 * Set the DAC rate. Assumes the codec supports VRA. The caller is
1329 * expected to have checked this little detail.
1330 */
1331
1332unsigned int ac97_set_dac_rate(struct ac97_codec *codec, unsigned int rate)
1333{
1334 unsigned int new_rate = rate;
1335 u32 dacp;
1336 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
1337 u32 mute_vol = 0x8000; /* The mute volume? */
1338
1339 if(rate != codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE))
1340 {
1341 /* Mute several registers */
1342 mast_vol = codec->codec_read(codec, AC97_MASTER_VOL_STEREO);
1343 mono_vol = codec->codec_read(codec, AC97_MASTER_VOL_MONO);
1344 phone_vol = codec->codec_read(codec, AC97_HEADPHONE_VOL);
1345 pcm_vol = codec->codec_read(codec, AC97_PCMOUT_VOL);
1346 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mute_vol);
1347 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mute_vol);
1348 codec->codec_write(codec, AC97_HEADPHONE_VOL, mute_vol);
1349 codec->codec_write(codec, AC97_PCMOUT_VOL, mute_vol);
1350
1351 /* Power down the DAC */
1352 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1353 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0200);
1354 /* Load the rate and read the effective rate */
1355 codec->codec_write(codec, AC97_PCM_FRONT_DAC_RATE, rate);
1356 new_rate=codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE);
1357 /* Power it back up */
1358 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1359
1360 /* Restore volumes */
1361 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mast_vol);
1362 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mono_vol);
1363 codec->codec_write(codec, AC97_HEADPHONE_VOL, phone_vol);
1364 codec->codec_write(codec, AC97_PCMOUT_VOL, pcm_vol);
1365 }
1366 return new_rate;
1367}
1368
1369EXPORT_SYMBOL(ac97_set_dac_rate);
1370
1371/**
1372 * ac97_set_adc_rate - set codec rate adaption
1373 * @codec: ac97 code
1374 * @rate: rate in hertz
1375 *
1376 * Set the ADC rate. Assumes the codec supports VRA. The caller is
1377 * expected to have checked this little detail.
1378 */
1379
1380unsigned int ac97_set_adc_rate(struct ac97_codec *codec, unsigned int rate)
1381{
1382 unsigned int new_rate = rate;
1383 u32 dacp;
1384
1385 if(rate != codec->codec_read(codec, AC97_PCM_LR_ADC_RATE))
1386 {
1387 /* Power down the ADC */
1388 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1389 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0100);
1390 /* Load the rate and read the effective rate */
1391 codec->codec_write(codec, AC97_PCM_LR_ADC_RATE, rate);
1392 new_rate=codec->codec_read(codec, AC97_PCM_LR_ADC_RATE);
1393 /* Power it back up */
1394 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1395 }
1396 return new_rate;
1397}
1398
1399EXPORT_SYMBOL(ac97_set_adc_rate);
1400
1401static int swap_headphone(int remove_master)
1402{
1403 struct list_head *l;
1404 struct ac97_codec *c;
1405
1406 if (remove_master) {
1407 mutex_lock(&codec_mutex);
1408 list_for_each(l, &codecs)
1409 {
1410 c = list_entry(l, struct ac97_codec, list);
1411 if (supported_mixer(c, SOUND_MIXER_PHONEOUT))
1412 c->supported_mixers &= ~SOUND_MASK_PHONEOUT;
1413 }
1414 mutex_unlock(&codec_mutex);
1415 } else
1416 ac97_hw[SOUND_MIXER_PHONEOUT].offset = AC97_MASTER_VOL_STEREO;
1417
1418 /* Scale values already match */
1419 ac97_hw[SOUND_MIXER_VOLUME].offset = AC97_MASTER_VOL_MONO;
1420 return 0;
1421}
1422
1423static int apply_quirk(int quirk)
1424{
1425 switch (quirk) {
1426 case AC97_TUNE_NONE:
1427 return 0;
1428 case AC97_TUNE_HP_ONLY:
1429 return swap_headphone(1);
1430 case AC97_TUNE_SWAP_HP:
1431 return swap_headphone(0);
1432 case AC97_TUNE_SWAP_SURROUND:
1433 return -ENOSYS; /* not yet implemented */
1434 case AC97_TUNE_AD_SHARING:
1435 return -ENOSYS; /* not yet implemented */
1436 case AC97_TUNE_ALC_JACK:
1437 return -ENOSYS; /* not yet implemented */
1438 }
1439 return -EINVAL;
1440}
1441
1442/**
1443 * ac97_tune_hardware - tune up the hardware
1444 * @pdev: pci_dev pointer
1445 * @quirk: quirk list
1446 * @override: explicit quirk value (overrides if not AC97_TUNE_DEFAULT)
1447 *
1448 * Do some workaround for each pci device, such as renaming of the
1449 * headphone (true line-out) control as "Master".
1450 * The quirk-list must be terminated with a zero-filled entry.
1451 *
1452 * Returns zero if successful, or a negative error code on failure.
1453 */
1454
1455int ac97_tune_hardware(struct pci_dev *pdev, struct ac97_quirk *quirk, int override)
1456{
1457 int result;
1458
1459 if (!quirk)
1460 return -EINVAL;
1461
1462 if (override != AC97_TUNE_DEFAULT) {
1463 result = apply_quirk(override);
1464 if (result < 0)
1465 printk(KERN_ERR "applying quirk type %d failed (%d)\n", override, result);
1466 return result;
1467 }
1468
1469 for (; quirk->vendor; quirk++) {
1470 if (quirk->vendor != pdev->subsystem_vendor)
1471 continue;
1472 if ((! quirk->mask && quirk->device == pdev->subsystem_device) ||
1473 quirk->device == (quirk->mask & pdev->subsystem_device)) {
1474#ifdef DEBUG
1475 printk("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, pdev->subsystem_device);
1476#endif
1477 result = apply_quirk(quirk->type);
1478 if (result < 0)
1479 printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
1480 return result;
1481 }
1482 }
1483 return 0;
1484}
1485
1486EXPORT_SYMBOL_GPL(ac97_tune_hardware);
1487
1488MODULE_LICENSE("GPL");
diff --git a/sound/oss/btaudio.c b/sound/oss/btaudio.c
deleted file mode 100644
index 4d5cf05b8922..000000000000
--- a/sound/oss/btaudio.c
+++ /dev/null
@@ -1,1139 +0,0 @@
1/*
2 btaudio - bt878 audio dma driver for linux 2.4.x
3
4 (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20*/
21
22#include <linux/module.h>
23#include <linux/errno.h>
24#include <linux/pci.h>
25#include <linux/sched.h>
26#include <linux/signal.h>
27#include <linux/types.h>
28#include <linux/interrupt.h>
29#include <linux/init.h>
30#include <linux/poll.h>
31#include <linux/sound.h>
32#include <linux/soundcard.h>
33#include <linux/slab.h>
34#include <linux/kdev_t.h>
35#include <linux/mutex.h>
36
37#include <asm/uaccess.h>
38#include <asm/io.h>
39
40
41/* mmio access */
42#define btwrite(dat,adr) writel((dat), (bta->mmio+(adr)))
43#define btread(adr) readl(bta->mmio+(adr))
44
45#define btand(dat,adr) btwrite((dat) & btread(adr), adr)
46#define btor(dat,adr) btwrite((dat) | btread(adr), adr)
47#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
48
49/* registers (shifted because bta->mmio is long) */
50#define REG_INT_STAT (0x100 >> 2)
51#define REG_INT_MASK (0x104 >> 2)
52#define REG_GPIO_DMA_CTL (0x10c >> 2)
53#define REG_PACKET_LEN (0x110 >> 2)
54#define REG_RISC_STRT_ADD (0x114 >> 2)
55#define REG_RISC_COUNT (0x120 >> 2)
56
57/* IRQ bits - REG_INT_(STAT|MASK) */
58#define IRQ_SCERR (1 << 19)
59#define IRQ_OCERR (1 << 18)
60#define IRQ_PABORT (1 << 17)
61#define IRQ_RIPERR (1 << 16)
62#define IRQ_PPERR (1 << 15)
63#define IRQ_FDSR (1 << 14)
64#define IRQ_FTRGT (1 << 13)
65#define IRQ_FBUS (1 << 12)
66#define IRQ_RISCI (1 << 11)
67#define IRQ_OFLOW (1 << 3)
68
69#define IRQ_BTAUDIO (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
70 IRQ_PPERR | IRQ_FDSR | IRQ_FTRGT | IRQ_FBUS |\
71 IRQ_RISCI)
72
73/* REG_GPIO_DMA_CTL bits */
74#define DMA_CTL_A_PWRDN (1 << 26)
75#define DMA_CTL_DA_SBR (1 << 14)
76#define DMA_CTL_DA_ES2 (1 << 13)
77#define DMA_CTL_ACAP_EN (1 << 4)
78#define DMA_CTL_RISC_EN (1 << 1)
79#define DMA_CTL_FIFO_EN (1 << 0)
80
81/* RISC instructions */
82#define RISC_WRITE (0x01 << 28)
83#define RISC_JUMP (0x07 << 28)
84#define RISC_SYNC (0x08 << 28)
85
86/* RISC bits */
87#define RISC_WR_SOL (1 << 27)
88#define RISC_WR_EOL (1 << 26)
89#define RISC_IRQ (1 << 24)
90#define RISC_SYNC_RESYNC (1 << 15)
91#define RISC_SYNC_FM1 0x06
92#define RISC_SYNC_VRO 0x0c
93
94#define HWBASE_AD (448000)
95
96/* -------------------------------------------------------------- */
97
98struct btaudio {
99 /* linked list */
100 struct btaudio *next;
101
102 /* device info */
103 int dsp_digital;
104 int dsp_analog;
105 int mixer_dev;
106 struct pci_dev *pci;
107 unsigned int irq;
108 unsigned long mem;
109 unsigned long __iomem *mmio;
110
111 /* locking */
112 int users;
113 struct mutex lock;
114
115 /* risc instructions */
116 unsigned int risc_size;
117 unsigned long *risc_cpu;
118 dma_addr_t risc_dma;
119
120 /* audio data */
121 unsigned int buf_size;
122 unsigned char *buf_cpu;
123 dma_addr_t buf_dma;
124
125 /* buffer setup */
126 int line_bytes;
127 int line_count;
128 int block_bytes;
129 int block_count;
130
131 /* read fifo management */
132 int recording;
133 int dma_block;
134 int read_offset;
135 int read_count;
136 wait_queue_head_t readq;
137
138 /* settings */
139 int gain[3];
140 int source;
141 int bits;
142 int decimation;
143 int mixcount;
144 int sampleshift;
145 int channels;
146 int analog;
147 int rate;
148};
149
150struct cardinfo {
151 char *name;
152 int rate;
153};
154
155static struct btaudio *btaudios;
156static unsigned int debug;
157static unsigned int irq_debug;
158
159/* -------------------------------------------------------------- */
160
161#define BUF_DEFAULT 128*1024
162#define BUF_MIN 8192
163
164static int alloc_buffer(struct btaudio *bta)
165{
166 if (NULL == bta->buf_cpu) {
167 for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN;
168 bta->buf_size = bta->buf_size >> 1) {
169 bta->buf_cpu = pci_alloc_consistent
170 (bta->pci, bta->buf_size, &bta->buf_dma);
171 if (NULL != bta->buf_cpu)
172 break;
173 }
174 if (NULL == bta->buf_cpu)
175 return -ENOMEM;
176 memset(bta->buf_cpu,0,bta->buf_size);
177 }
178 if (NULL == bta->risc_cpu) {
179 bta->risc_size = PAGE_SIZE;
180 bta->risc_cpu = pci_alloc_consistent
181 (bta->pci, bta->risc_size, &bta->risc_dma);
182 if (NULL == bta->risc_cpu) {
183 pci_free_consistent(bta->pci, bta->buf_size, bta->buf_cpu, bta->buf_dma);
184 bta->buf_cpu = NULL;
185 return -ENOMEM;
186 }
187 }
188 return 0;
189}
190
191static void free_buffer(struct btaudio *bta)
192{
193 if (NULL != bta->buf_cpu) {
194 pci_free_consistent(bta->pci, bta->buf_size,
195 bta->buf_cpu, bta->buf_dma);
196 bta->buf_cpu = NULL;
197 }
198 if (NULL != bta->risc_cpu) {
199 pci_free_consistent(bta->pci, bta->risc_size,
200 bta->risc_cpu, bta->risc_dma);
201 bta->risc_cpu = NULL;
202 }
203}
204
205static int make_risc(struct btaudio *bta)
206{
207 int rp, bp, line, block;
208 unsigned long risc;
209
210 bta->block_bytes = bta->buf_size >> 4;
211 bta->block_count = 1 << 4;
212 bta->line_bytes = bta->block_bytes;
213 bta->line_count = bta->block_count;
214 while (bta->line_bytes > 4095) {
215 bta->line_bytes >>= 1;
216 bta->line_count <<= 1;
217 }
218 if (bta->line_count > 255)
219 return -EINVAL;
220 if (debug)
221 printk(KERN_DEBUG
222 "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n",
223 bta->buf_size,bta->block_bytes,bta->block_count,
224 bta->line_bytes,bta->line_count);
225 rp = 0; bp = 0;
226 block = 0;
227 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1);
228 bta->risc_cpu[rp++] = cpu_to_le32(0);
229 for (line = 0; line < bta->line_count; line++) {
230 risc = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL;
231 risc |= bta->line_bytes;
232 if (0 == (bp & (bta->block_bytes-1))) {
233 risc |= RISC_IRQ;
234 risc |= (block & 0x0f) << 16;
235 risc |= (~block & 0x0f) << 20;
236 block++;
237 }
238 bta->risc_cpu[rp++] = cpu_to_le32(risc);
239 bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp);
240 bp += bta->line_bytes;
241 }
242 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO);
243 bta->risc_cpu[rp++] = cpu_to_le32(0);
244 bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP);
245 bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma);
246 return 0;
247}
248
249static int start_recording(struct btaudio *bta)
250{
251 int ret;
252
253 if (0 != (ret = alloc_buffer(bta)))
254 return ret;
255 if (0 != (ret = make_risc(bta)))
256 return ret;
257
258 btwrite(bta->risc_dma, REG_RISC_STRT_ADD);
259 btwrite((bta->line_count << 16) | bta->line_bytes,
260 REG_PACKET_LEN);
261 btwrite(IRQ_BTAUDIO, REG_INT_MASK);
262 if (bta->analog) {
263 btwrite(DMA_CTL_ACAP_EN |
264 DMA_CTL_RISC_EN |
265 DMA_CTL_FIFO_EN |
266 DMA_CTL_DA_ES2 |
267 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
268 (bta->gain[bta->source] << 28) |
269 (bta->source << 24) |
270 (bta->decimation << 8),
271 REG_GPIO_DMA_CTL);
272 } else {
273 btwrite(DMA_CTL_ACAP_EN |
274 DMA_CTL_RISC_EN |
275 DMA_CTL_FIFO_EN |
276 DMA_CTL_DA_ES2 |
277 DMA_CTL_A_PWRDN |
278 (1 << 6) |
279 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
280 (bta->gain[bta->source] << 28) |
281 (bta->source << 24) |
282 (bta->decimation << 8),
283 REG_GPIO_DMA_CTL);
284 }
285 bta->dma_block = 0;
286 bta->read_offset = 0;
287 bta->read_count = 0;
288 bta->recording = 1;
289 if (debug)
290 printk(KERN_DEBUG "btaudio: recording started\n");
291 return 0;
292}
293
294static void stop_recording(struct btaudio *bta)
295{
296 btand(~15, REG_GPIO_DMA_CTL);
297 bta->recording = 0;
298 if (debug)
299 printk(KERN_DEBUG "btaudio: recording stopped\n");
300}
301
302
303/* -------------------------------------------------------------- */
304
305static int btaudio_mixer_open(struct inode *inode, struct file *file)
306{
307 int minor = iminor(inode);
308 struct btaudio *bta;
309
310 for (bta = btaudios; bta != NULL; bta = bta->next)
311 if (bta->mixer_dev == minor)
312 break;
313 if (NULL == bta)
314 return -ENODEV;
315
316 if (debug)
317 printk("btaudio: open mixer [%d]\n",minor);
318 file->private_data = bta;
319 return 0;
320}
321
322static int btaudio_mixer_release(struct inode *inode, struct file *file)
323{
324 return 0;
325}
326
327static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
328 unsigned int cmd, unsigned long arg)
329{
330 struct btaudio *bta = file->private_data;
331 int ret,val=0,i=0;
332 void __user *argp = (void __user *)arg;
333
334 if (cmd == SOUND_MIXER_INFO) {
335 mixer_info info;
336 memset(&info,0,sizeof(info));
337 strlcpy(info.id,"bt878",sizeof(info.id));
338 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
339 info.modify_counter = bta->mixcount;
340 if (copy_to_user(argp, &info, sizeof(info)))
341 return -EFAULT;
342 return 0;
343 }
344 if (cmd == SOUND_OLD_MIXER_INFO) {
345 _old_mixer_info info;
346 memset(&info,0,sizeof(info));
347 strlcpy(info.id, "bt878", sizeof(info.id));
348 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
349 if (copy_to_user(argp, &info, sizeof(info)))
350 return -EFAULT;
351 return 0;
352 }
353 if (cmd == OSS_GETVERSION)
354 return put_user(SOUND_VERSION, (int __user *)argp);
355
356 /* read */
357 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
358 if (get_user(val, (int __user *)argp))
359 return -EFAULT;
360
361 switch (cmd) {
362 case MIXER_READ(SOUND_MIXER_CAPS):
363 ret = SOUND_CAP_EXCL_INPUT;
364 break;
365 case MIXER_READ(SOUND_MIXER_STEREODEVS):
366 ret = 0;
367 break;
368 case MIXER_READ(SOUND_MIXER_RECMASK):
369 case MIXER_READ(SOUND_MIXER_DEVMASK):
370 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
371 break;
372
373 case MIXER_WRITE(SOUND_MIXER_RECSRC):
374 if (val & SOUND_MASK_LINE1 && bta->source != 0)
375 bta->source = 0;
376 else if (val & SOUND_MASK_LINE2 && bta->source != 1)
377 bta->source = 1;
378 else if (val & SOUND_MASK_LINE3 && bta->source != 2)
379 bta->source = 2;
380 btaor((bta->gain[bta->source] << 28) |
381 (bta->source << 24),
382 0x0cffffff, REG_GPIO_DMA_CTL);
383 case MIXER_READ(SOUND_MIXER_RECSRC):
384 switch (bta->source) {
385 case 0: ret = SOUND_MASK_LINE1; break;
386 case 1: ret = SOUND_MASK_LINE2; break;
387 case 2: ret = SOUND_MASK_LINE3; break;
388 default: ret = 0;
389 }
390 break;
391
392 case MIXER_WRITE(SOUND_MIXER_LINE1):
393 case MIXER_WRITE(SOUND_MIXER_LINE2):
394 case MIXER_WRITE(SOUND_MIXER_LINE3):
395 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
396 i = 0;
397 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
398 i = 1;
399 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
400 i = 2;
401 bta->gain[i] = (val & 0xff) * 15 / 100;
402 if (bta->gain[i] > 15) bta->gain[i] = 15;
403 if (bta->gain[i] < 0) bta->gain[i] = 0;
404 if (i == bta->source)
405 btaor((bta->gain[bta->source]<<28),
406 0x0fffffff, REG_GPIO_DMA_CTL);
407 ret = bta->gain[i] * 100 / 15;
408 ret |= ret << 8;
409 break;
410
411 case MIXER_READ(SOUND_MIXER_LINE1):
412 case MIXER_READ(SOUND_MIXER_LINE2):
413 case MIXER_READ(SOUND_MIXER_LINE3):
414 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
415 i = 0;
416 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
417 i = 1;
418 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
419 i = 2;
420 ret = bta->gain[i] * 100 / 15;
421 ret |= ret << 8;
422 break;
423
424 default:
425 return -EINVAL;
426 }
427 if (put_user(ret, (int __user *)argp))
428 return -EFAULT;
429 return 0;
430}
431
432static const struct file_operations btaudio_mixer_fops = {
433 .owner = THIS_MODULE,
434 .llseek = no_llseek,
435 .open = btaudio_mixer_open,
436 .release = btaudio_mixer_release,
437 .ioctl = btaudio_mixer_ioctl,
438};
439
440/* -------------------------------------------------------------- */
441
442static int btaudio_dsp_open(struct inode *inode, struct file *file,
443 struct btaudio *bta, int analog)
444{
445 mutex_lock(&bta->lock);
446 if (bta->users)
447 goto busy;
448 bta->users++;
449 file->private_data = bta;
450
451 bta->analog = analog;
452 bta->dma_block = 0;
453 bta->read_offset = 0;
454 bta->read_count = 0;
455 bta->sampleshift = 0;
456
457 mutex_unlock(&bta->lock);
458 return 0;
459
460 busy:
461 mutex_unlock(&bta->lock);
462 return -EBUSY;
463}
464
465static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
466{
467 int minor = iminor(inode);
468 struct btaudio *bta;
469
470 for (bta = btaudios; bta != NULL; bta = bta->next)
471 if (bta->dsp_digital == minor)
472 break;
473 if (NULL == bta)
474 return -ENODEV;
475
476 if (debug)
477 printk("btaudio: open digital dsp [%d]\n",minor);
478 return btaudio_dsp_open(inode,file,bta,0);
479}
480
481static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
482{
483 int minor = iminor(inode);
484 struct btaudio *bta;
485
486 for (bta = btaudios; bta != NULL; bta = bta->next)
487 if (bta->dsp_analog == minor)
488 break;
489 if (NULL == bta)
490 return -ENODEV;
491
492 if (debug)
493 printk("btaudio: open analog dsp [%d]\n",minor);
494 return btaudio_dsp_open(inode,file,bta,1);
495}
496
497static int btaudio_dsp_release(struct inode *inode, struct file *file)
498{
499 struct btaudio *bta = file->private_data;
500
501 mutex_lock(&bta->lock);
502 if (bta->recording)
503 stop_recording(bta);
504 bta->users--;
505 mutex_unlock(&bta->lock);
506 return 0;
507}
508
509static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer,
510 size_t swcount, loff_t *ppos)
511{
512 struct btaudio *bta = file->private_data;
513 int hwcount = swcount << bta->sampleshift;
514 int nsrc, ndst, err, ret = 0;
515 DECLARE_WAITQUEUE(wait, current);
516
517 add_wait_queue(&bta->readq, &wait);
518 mutex_lock(&bta->lock);
519 while (swcount > 0) {
520 if (0 == bta->read_count) {
521 if (!bta->recording) {
522 if (0 != (err = start_recording(bta))) {
523 if (0 == ret)
524 ret = err;
525 break;
526 }
527 }
528 if (file->f_flags & O_NONBLOCK) {
529 if (0 == ret)
530 ret = -EAGAIN;
531 break;
532 }
533 mutex_unlock(&bta->lock);
534 current->state = TASK_INTERRUPTIBLE;
535 schedule();
536 mutex_lock(&bta->lock);
537 if(signal_pending(current)) {
538 if (0 == ret)
539 ret = -EINTR;
540 break;
541 }
542 }
543 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
544 if (nsrc > bta->buf_size - bta->read_offset)
545 nsrc = bta->buf_size - bta->read_offset;
546 ndst = nsrc >> bta->sampleshift;
547
548 if ((bta->analog && 0 == bta->sampleshift) ||
549 (!bta->analog && 2 == bta->channels)) {
550 /* just copy */
551 if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
552 if (0 == ret)
553 ret = -EFAULT;
554 break;
555 }
556
557 } else if (!bta->analog) {
558 /* stereo => mono (digital audio) */
559 __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
560 __s16 __user *dst = (__s16 __user *)(buffer + ret);
561 __s16 avg;
562 int n = ndst>>1;
563 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
564 if (0 == ret)
565 ret = -EFAULT;
566 break;
567 }
568 for (; n; n--, dst++) {
569 avg = (__s16)le16_to_cpu(*src) / 2; src++;
570 avg += (__s16)le16_to_cpu(*src) / 2; src++;
571 __put_user(cpu_to_le16(avg),dst);
572 }
573
574 } else if (8 == bta->bits) {
575 /* copy + byte downsampling (audio A/D) */
576 __u8 *src = bta->buf_cpu + bta->read_offset;
577 __u8 __user *dst = buffer + ret;
578 int n = ndst;
579 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
580 if (0 == ret)
581 ret = -EFAULT;
582 break;
583 }
584 for (; n; n--, src += (1 << bta->sampleshift), dst++)
585 __put_user(*src, dst);
586
587 } else {
588 /* copy + word downsampling (audio A/D) */
589 __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
590 __u16 __user *dst = (__u16 __user *)(buffer + ret);
591 int n = ndst>>1;
592 if (!access_ok(VERIFY_WRITE,dst,ndst)) {
593 if (0 == ret)
594 ret = -EFAULT;
595 break;
596 }
597 for (; n; n--, src += (1 << bta->sampleshift), dst++)
598 __put_user(*src, dst);
599 }
600
601 ret += ndst;
602 swcount -= ndst;
603 hwcount -= nsrc;
604 bta->read_count -= nsrc;
605 bta->read_offset += nsrc;
606 if (bta->read_offset == bta->buf_size)
607 bta->read_offset = 0;
608 }
609 mutex_unlock(&bta->lock);
610 remove_wait_queue(&bta->readq, &wait);
611 current->state = TASK_RUNNING;
612 return ret;
613}
614
615static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer,
616 size_t count, loff_t *ppos)
617{
618 return -EINVAL;
619}
620
621static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
622 unsigned int cmd, unsigned long arg)
623{
624 struct btaudio *bta = file->private_data;
625 int s, i, ret, val = 0;
626 void __user *argp = (void __user *)arg;
627 int __user *p = argp;
628
629 switch (cmd) {
630 case OSS_GETVERSION:
631 return put_user(SOUND_VERSION, p);
632 case SNDCTL_DSP_GETCAPS:
633 return 0;
634
635 case SNDCTL_DSP_SPEED:
636 if (get_user(val, p))
637 return -EFAULT;
638 if (bta->analog) {
639 for (s = 0; s < 16; s++)
640 if (val << s >= HWBASE_AD*4/15)
641 break;
642 for (i = 15; i >= 5; i--)
643 if (val << s <= HWBASE_AD*4/i)
644 break;
645 bta->sampleshift = s;
646 bta->decimation = i;
647 if (debug)
648 printk(KERN_DEBUG "btaudio: rate: req=%d "
649 "dec=%d shift=%d hwrate=%d swrate=%d\n",
650 val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
651 } else {
652 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
653 bta->decimation = 0;
654 }
655 if (bta->recording) {
656 mutex_lock(&bta->lock);
657 stop_recording(bta);
658 start_recording(bta);
659 mutex_unlock(&bta->lock);
660 }
661 /* fall through */
662 case SOUND_PCM_READ_RATE:
663 if (bta->analog) {
664 return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, p);
665 } else {
666 return put_user(bta->rate, p);
667 }
668
669 case SNDCTL_DSP_STEREO:
670 if (!bta->analog) {
671 if (get_user(val, p))
672 return -EFAULT;
673 bta->channels = (val > 0) ? 2 : 1;
674 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
675 if (debug)
676 printk(KERN_INFO
677 "btaudio: stereo=%d channels=%d\n",
678 val,bta->channels);
679 } else {
680 if (val == 1)
681 return -EFAULT;
682 else {
683 bta->channels = 1;
684 if (debug)
685 printk(KERN_INFO
686 "btaudio: stereo=0 channels=1\n");
687 }
688 }
689 return put_user((bta->channels)-1, p);
690
691 case SNDCTL_DSP_CHANNELS:
692 if (!bta->analog) {
693 if (get_user(val, p))
694 return -EFAULT;
695 bta->channels = (val > 1) ? 2 : 1;
696 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
697 if (debug)
698 printk(KERN_DEBUG
699 "btaudio: val=%d channels=%d\n",
700 val,bta->channels);
701 }
702 /* fall through */
703 case SOUND_PCM_READ_CHANNELS:
704 return put_user(bta->channels, p);
705
706 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
707 if (bta->analog)
708 return put_user(AFMT_S16_LE|AFMT_S8, p);
709 else
710 return put_user(AFMT_S16_LE, p);
711
712 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
713 if (get_user(val, p))
714 return -EFAULT;
715 if (val != AFMT_QUERY) {
716 if (bta->analog)
717 bta->bits = (val == AFMT_S8) ? 8 : 16;
718 else
719 bta->bits = 16;
720 if (bta->recording) {
721 mutex_lock(&bta->lock);
722 stop_recording(bta);
723 start_recording(bta);
724 mutex_unlock(&bta->lock);
725 }
726 }
727 if (debug)
728 printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
729 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
730 p);
731 break;
732 case SOUND_PCM_READ_BITS:
733 return put_user(bta->bits, p);
734
735 case SNDCTL_DSP_NONBLOCK:
736 file->f_flags |= O_NONBLOCK;
737 return 0;
738
739 case SNDCTL_DSP_RESET:
740 if (bta->recording) {
741 mutex_lock(&bta->lock);
742 stop_recording(bta);
743 mutex_unlock(&bta->lock);
744 }
745 return 0;
746 case SNDCTL_DSP_GETBLKSIZE:
747 if (!bta->recording) {
748 if (0 != (ret = alloc_buffer(bta)))
749 return ret;
750 if (0 != (ret = make_risc(bta)))
751 return ret;
752 }
753 return put_user(bta->block_bytes>>bta->sampleshift,p);
754
755 case SNDCTL_DSP_SYNC:
756 /* NOP */
757 return 0;
758 case SNDCTL_DSP_GETISPACE:
759 {
760 audio_buf_info info;
761 if (!bta->recording)
762 return -EINVAL;
763 info.fragsize = bta->block_bytes>>bta->sampleshift;
764 info.fragstotal = bta->block_count;
765 info.bytes = bta->read_count;
766 info.fragments = info.bytes / info.fragsize;
767 if (debug)
768 printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
769 "returns %d/%d/%d/%d\n",
770 info.fragsize, info.fragstotal,
771 info.bytes, info.fragments);
772 if (copy_to_user(argp, &info, sizeof(info)))
773 return -EFAULT;
774 return 0;
775 }
776#if 0 /* TODO */
777 case SNDCTL_DSP_GETTRIGGER:
778 case SNDCTL_DSP_SETTRIGGER:
779 case SNDCTL_DSP_SETFRAGMENT:
780#endif
781 default:
782 return -EINVAL;
783 }
784}
785
786static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
787{
788 struct btaudio *bta = file->private_data;
789 unsigned int mask = 0;
790
791 poll_wait(file, &bta->readq, wait);
792
793 if (0 != bta->read_count)
794 mask |= (POLLIN | POLLRDNORM);
795
796 return mask;
797}
798
799static const struct file_operations btaudio_digital_dsp_fops = {
800 .owner = THIS_MODULE,
801 .llseek = no_llseek,
802 .open = btaudio_dsp_open_digital,
803 .release = btaudio_dsp_release,
804 .read = btaudio_dsp_read,
805 .write = btaudio_dsp_write,
806 .ioctl = btaudio_dsp_ioctl,
807 .poll = btaudio_dsp_poll,
808};
809
810static const struct file_operations btaudio_analog_dsp_fops = {
811 .owner = THIS_MODULE,
812 .llseek = no_llseek,
813 .open = btaudio_dsp_open_analog,
814 .release = btaudio_dsp_release,
815 .read = btaudio_dsp_read,
816 .write = btaudio_dsp_write,
817 .ioctl = btaudio_dsp_ioctl,
818 .poll = btaudio_dsp_poll,
819};
820
821/* -------------------------------------------------------------- */
822
823static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
824 "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
825 "RIPERR", "PABORT", "OCERR", "SCERR" };
826
827static irqreturn_t btaudio_irq(int irq, void *dev_id)
828{
829 int count = 0;
830 u32 stat,astat;
831 struct btaudio *bta = dev_id;
832 int handled = 0;
833
834 for (;;) {
835 count++;
836 stat = btread(REG_INT_STAT);
837 astat = stat & btread(REG_INT_MASK);
838 if (!astat)
839 return IRQ_RETVAL(handled);
840 handled = 1;
841 btwrite(astat,REG_INT_STAT);
842
843 if (irq_debug) {
844 int i;
845 printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
846 count, stat>>28);
847 for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
848 if (stat & (1 << i))
849 printk(" %s",irq_name[i]);
850 if (astat & (1 << i))
851 printk("*");
852 }
853 printk("\n");
854 }
855 if (stat & IRQ_RISCI) {
856 int blocks;
857 blocks = (stat >> 28) - bta->dma_block;
858 if (blocks < 0)
859 blocks += bta->block_count;
860 bta->dma_block = stat >> 28;
861 if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
862 stop_recording(bta);
863 printk(KERN_INFO "btaudio: buffer overrun\n");
864 }
865 if (blocks > 0) {
866 bta->read_count += blocks * bta->block_bytes;
867 wake_up_interruptible(&bta->readq);
868 }
869 }
870 if (count > 10) {
871 printk(KERN_WARNING
872 "btaudio: Oops - irq mask cleared\n");
873 btwrite(0, REG_INT_MASK);
874 }
875 }
876 return IRQ_NONE;
877}
878
879/* -------------------------------------------------------------- */
880
881static unsigned int dsp1 = -1;
882static unsigned int dsp2 = -1;
883static unsigned int mixer = -1;
884static int latency = -1;
885static int digital = 1;
886static int analog = 1;
887static int rate;
888
889#define BTA_OSPREY200 1
890
891static struct cardinfo cards[] = {
892 [0] = {
893 .name = "default",
894 .rate = 32000,
895 },
896 [BTA_OSPREY200] = {
897 .name = "Osprey 200",
898 .rate = 44100,
899 },
900};
901
902static int __devinit btaudio_probe(struct pci_dev *pci_dev,
903 const struct pci_device_id *pci_id)
904{
905 struct btaudio *bta;
906 struct cardinfo *card = &cards[pci_id->driver_data];
907 unsigned char revision,lat;
908 int rc = -EBUSY;
909
910 if (pci_enable_device(pci_dev))
911 return -EIO;
912 if (!request_mem_region(pci_resource_start(pci_dev,0),
913 pci_resource_len(pci_dev,0),
914 "btaudio")) {
915 return -EBUSY;
916 }
917
918 bta = kzalloc(sizeof(*bta),GFP_ATOMIC);
919 if (!bta) {
920 rc = -ENOMEM;
921 goto fail0;
922 }
923
924 bta->pci = pci_dev;
925 bta->irq = pci_dev->irq;
926 bta->mem = pci_resource_start(pci_dev,0);
927 bta->mmio = ioremap(pci_resource_start(pci_dev,0),
928 pci_resource_len(pci_dev,0));
929
930 bta->source = 1;
931 bta->bits = 8;
932 bta->channels = 1;
933 if (bta->analog) {
934 bta->decimation = 15;
935 } else {
936 bta->decimation = 0;
937 bta->sampleshift = 1;
938 }
939
940 /* sample rate */
941 bta->rate = card->rate;
942 if (rate)
943 bta->rate = rate;
944
945 mutex_init(&bta->lock);
946 init_waitqueue_head(&bta->readq);
947
948 if (-1 != latency) {
949 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
950 latency);
951 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
952 }
953 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
954 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
955 printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
956 pci_dev->device,revision,pci_dev->bus->number,
957 PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
958 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
959 bta->irq, lat, bta->mem);
960 printk("btaudio: using card config \"%s\"\n", card->name);
961
962 /* init hw */
963 btwrite(0, REG_GPIO_DMA_CTL);
964 btwrite(0, REG_INT_MASK);
965 btwrite(~0U, REG_INT_STAT);
966 pci_set_master(pci_dev);
967
968 if ((rc = request_irq(bta->irq, btaudio_irq, IRQF_SHARED|IRQF_DISABLED,
969 "btaudio",(void *)bta)) < 0) {
970 printk(KERN_WARNING
971 "btaudio: can't request irq (rc=%d)\n",rc);
972 goto fail1;
973 }
974
975 /* register devices */
976 if (digital) {
977 rc = bta->dsp_digital =
978 register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
979 if (rc < 0) {
980 printk(KERN_WARNING
981 "btaudio: can't register digital dsp (rc=%d)\n",rc);
982 goto fail2;
983 }
984 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
985 bta->dsp_digital >> 4);
986 }
987 if (analog) {
988 rc = bta->dsp_analog =
989 register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
990 if (rc < 0) {
991 printk(KERN_WARNING
992 "btaudio: can't register analog dsp (rc=%d)\n",rc);
993 goto fail3;
994 }
995 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
996 bta->dsp_analog >> 4);
997 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
998 if (rc < 0) {
999 printk(KERN_WARNING
1000 "btaudio: can't register mixer (rc=%d)\n",rc);
1001 goto fail4;
1002 }
1003 printk(KERN_INFO "btaudio: registered device mixer%d\n",
1004 bta->mixer_dev >> 4);
1005 }
1006
1007 /* hook into linked list */
1008 bta->next = btaudios;
1009 btaudios = bta;
1010
1011 pci_set_drvdata(pci_dev,bta);
1012 return 0;
1013
1014 fail4:
1015 unregister_sound_dsp(bta->dsp_analog);
1016 fail3:
1017 if (digital)
1018 unregister_sound_dsp(bta->dsp_digital);
1019 fail2:
1020 free_irq(bta->irq,bta);
1021 fail1:
1022 iounmap(bta->mmio);
1023 kfree(bta);
1024 fail0:
1025 release_mem_region(pci_resource_start(pci_dev,0),
1026 pci_resource_len(pci_dev,0));
1027 return rc;
1028}
1029
1030static void __devexit btaudio_remove(struct pci_dev *pci_dev)
1031{
1032 struct btaudio *bta = pci_get_drvdata(pci_dev);
1033 struct btaudio *walk;
1034
1035 /* turn off all DMA / IRQs */
1036 btand(~15, REG_GPIO_DMA_CTL);
1037 btwrite(0, REG_INT_MASK);
1038 btwrite(~0U, REG_INT_STAT);
1039
1040 /* unregister devices */
1041 if (digital) {
1042 unregister_sound_dsp(bta->dsp_digital);
1043 }
1044 if (analog) {
1045 unregister_sound_dsp(bta->dsp_analog);
1046 unregister_sound_mixer(bta->mixer_dev);
1047 }
1048
1049 /* free resources */
1050 free_buffer(bta);
1051 free_irq(bta->irq,bta);
1052 release_mem_region(pci_resource_start(pci_dev,0),
1053 pci_resource_len(pci_dev,0));
1054 iounmap(bta->mmio);
1055
1056 /* remove from linked list */
1057 if (bta == btaudios) {
1058 btaudios = NULL;
1059 } else {
1060 for (walk = btaudios; walk->next != bta; walk = walk->next)
1061 ; /* if (NULL == walk->next) BUG(); */
1062 walk->next = bta->next;
1063 }
1064
1065 pci_set_drvdata(pci_dev, NULL);
1066 kfree(bta);
1067 return;
1068}
1069
1070/* -------------------------------------------------------------- */
1071
1072static struct pci_device_id btaudio_pci_tbl[] = {
1073 {
1074 .vendor = PCI_VENDOR_ID_BROOKTREE,
1075 .device = 0x0878,
1076 .subvendor = 0x0070,
1077 .subdevice = 0xff01,
1078 .driver_data = BTA_OSPREY200,
1079 },{
1080 .vendor = PCI_VENDOR_ID_BROOKTREE,
1081 .device = 0x0878,
1082 .subvendor = PCI_ANY_ID,
1083 .subdevice = PCI_ANY_ID,
1084 },{
1085 .vendor = PCI_VENDOR_ID_BROOKTREE,
1086 .device = 0x0878,
1087 .subvendor = PCI_ANY_ID,
1088 .subdevice = PCI_ANY_ID,
1089 },{
1090 /* --- end of list --- */
1091 }
1092};
1093
1094static struct pci_driver btaudio_pci_driver = {
1095 .name = "btaudio",
1096 .id_table = btaudio_pci_tbl,
1097 .probe = btaudio_probe,
1098 .remove = __devexit_p(btaudio_remove),
1099};
1100
1101static int btaudio_init_module(void)
1102{
1103 printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
1104 digital ? "digital" : "",
1105 analog && digital ? "+" : "",
1106 analog ? "analog" : "");
1107 return pci_register_driver(&btaudio_pci_driver);
1108}
1109
1110static void btaudio_cleanup_module(void)
1111{
1112 pci_unregister_driver(&btaudio_pci_driver);
1113 return;
1114}
1115
1116module_init(btaudio_init_module);
1117module_exit(btaudio_cleanup_module);
1118
1119module_param(dsp1, int, S_IRUGO);
1120module_param(dsp2, int, S_IRUGO);
1121module_param(mixer, int, S_IRUGO);
1122module_param(debug, int, S_IRUGO | S_IWUSR);
1123module_param(irq_debug, int, S_IRUGO | S_IWUSR);
1124module_param(digital, int, S_IRUGO);
1125module_param(analog, int, S_IRUGO);
1126module_param(rate, int, S_IRUGO);
1127module_param(latency, int, S_IRUGO);
1128MODULE_PARM_DESC(latency,"pci latency timer");
1129
1130MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1131MODULE_DESCRIPTION("bt878 audio dma driver");
1132MODULE_AUTHOR("Gerd Knorr");
1133MODULE_LICENSE("GPL");
1134
1135/*
1136 * Local variables:
1137 * c-basic-offset: 8
1138 * End:
1139 */
diff --git a/sound/oss/cs4232.c b/sound/oss/cs4232.c
deleted file mode 100644
index de40e21bf279..000000000000
--- a/sound/oss/cs4232.c
+++ /dev/null
@@ -1,526 +0,0 @@
1/*
2 * Copyright (C) by Hannu Savolainen 1993-1997
3 *
4 * cs4232.c
5 *
6 * The low level driver for Crystal CS4232 based cards. The CS4232 is
7 * a PnP compatible chip which contains a CS4231A codec, SB emulation,
8 * a MPU401 compatible MIDI port, joystick and synthesizer and IDE CD-ROM
9 * interfaces. This is just a temporary driver until full PnP support
10 * gets implemented. Just the WSS codec, FM synth and the MIDI ports are
11 * supported. Other interfaces are left uninitialized.
12 *
13 * ifdef ...WAVEFRONT...
14 *
15 * Support is provided for initializing the WaveFront synth
16 * interface as well, which is logical device #4. Note that if
17 * you have a Tropez+ card, you probably don't need to setup
18 * the CS4232-supported MIDI interface, since it corresponds to
19 * the internal 26-pin header that's hard to access. Using this
20 * requires an additional IRQ, a resource none too plentiful in
21 * this environment. Just don't set module parameters mpuio and
22 * mpuirq, and the MIDI port will be left uninitialized. You can
23 * still use the ICS2115 hosted MIDI interface which corresponds
24 * to the 9-pin D connector on the back of the card.
25 *
26 * endif ...WAVEFRONT...
27 *
28 * Supported chips are:
29 * CS4232
30 * CS4236
31 * CS4236B
32 *
33 * Note: You will need a PnP config setup to initialise some CS4232 boards
34 * anyway.
35 *
36 * Changes
37 * John Rood Added Bose Sound System Support.
38 * Toshio Spoor
39 * Alan Cox Modularisation, Basic cleanups.
40 * Paul Barton-Davis Separated MPU configuration, added
41 * Tropez+ (WaveFront) support
42 * Christoph Hellwig Adapted to module_init/module_exit,
43 * simple cleanups
44 * Arnaldo C. de Melo got rid of attach_uart401
45 * Bartlomiej Zolnierkiewicz
46 * Added some __init/__initdata/__exit
47 * Marcus Meissner Added ISA PnP support.
48 */
49
50#include <linux/pnp.h>
51#include <linux/module.h>
52#include <linux/init.h>
53
54#include "sound_config.h"
55
56#include "ad1848.h"
57#include "mpu401.h"
58
59#define KEY_PORT 0x279 /* Same as LPT1 status port */
60#define CSN_NUM 0x99 /* Just a random number */
61#define INDEX_ADDRESS 0x00 /* (R0) Index Address Register */
62#define INDEX_DATA 0x01 /* (R1) Indexed Data Register */
63#define PIN_CONTROL 0x0a /* (I10) Pin Control */
64#define ENABLE_PINS 0xc0 /* XCTRL0/XCTRL1 enable */
65
66static void CS_OUT(unsigned char a)
67{
68 outb(a, KEY_PORT);
69}
70
71#define CS_OUT2(a, b) {CS_OUT(a);CS_OUT(b);}
72#define CS_OUT3(a, b, c) {CS_OUT(a);CS_OUT(b);CS_OUT(c);}
73
74static int __initdata bss = 0;
75static int mpu_base, mpu_irq;
76static int synth_base, synth_irq;
77static int mpu_detected;
78
79static int probe_cs4232_mpu(struct address_info *hw_config)
80{
81 /*
82 * Just write down the config values.
83 */
84
85 mpu_base = hw_config->io_base;
86 mpu_irq = hw_config->irq;
87
88 return 1;
89}
90
91static unsigned char crystal_key[] = /* A 32 byte magic key sequence */
92{
93 0x96, 0x35, 0x9a, 0xcd, 0xe6, 0xf3, 0x79, 0xbc,
94 0x5e, 0xaf, 0x57, 0x2b, 0x15, 0x8a, 0xc5, 0xe2,
95 0xf1, 0xf8, 0x7c, 0x3e, 0x9f, 0x4f, 0x27, 0x13,
96 0x09, 0x84, 0x42, 0xa1, 0xd0, 0x68, 0x34, 0x1a
97};
98
99static void sleep(unsigned howlong)
100{
101 current->state = TASK_INTERRUPTIBLE;
102 schedule_timeout(howlong);
103}
104
105static void enable_xctrl(int baseio)
106{
107 unsigned char regd;
108
109 /*
110 * Some IBM Aptiva's have the Bose Sound System. By default
111 * the Bose Amplifier is disabled. The amplifier will be
112 * activated, by setting the XCTRL0 and XCTRL1 bits.
113 * Volume of the monitor bose speakers/woofer, can then
114 * be set by changing the PCM volume.
115 *
116 */
117
118 printk("cs4232: enabling Bose Sound System Amplifier.\n");
119
120 /* Switch to Pin Control Address */
121 regd = inb(baseio + INDEX_ADDRESS) & 0xe0;
122 outb(((unsigned char) (PIN_CONTROL | regd)), baseio + INDEX_ADDRESS );
123
124 /* Activate the XCTRL0 and XCTRL1 Pins */
125 regd = inb(baseio + INDEX_DATA);
126 outb(((unsigned char) (ENABLE_PINS | regd)), baseio + INDEX_DATA );
127}
128
129static int __init probe_cs4232(struct address_info *hw_config, int isapnp_configured)
130{
131 int i, n;
132 int base = hw_config->io_base, irq = hw_config->irq;
133 int dma1 = hw_config->dma, dma2 = hw_config->dma2;
134 struct resource *ports;
135
136 if (base == -1 || irq == -1 || dma1 == -1) {
137 printk(KERN_ERR "cs4232: dma, irq and io must be set.\n");
138 return 0;
139 }
140
141 /*
142 * Verify that the I/O port range is free.
143 */
144
145 ports = request_region(base, 4, "ad1848");
146 if (!ports) {
147 printk(KERN_ERR "cs4232.c: I/O port 0x%03x not free\n", base);
148 return 0;
149 }
150 if (ad1848_detect(ports, NULL, hw_config->osp)) {
151 goto got_it; /* The card is already active */
152 }
153 if (isapnp_configured) {
154 printk(KERN_ERR "cs4232.c: ISA PnP configured, but not detected?\n");
155 goto fail;
156 }
157
158 /*
159 * This version of the driver doesn't use the PnP method when configuring
160 * the card but a simplified method defined by Crystal. This means that
161 * just one CS4232 compatible device can exist on the system. Also this
162 * method conflicts with possible PnP support in the OS. For this reason
163 * driver is just a temporary kludge.
164 *
165 * Also the Cirrus/Crystal method doesn't always work. Try ISA PnP first ;)
166 */
167
168 /*
169 * Repeat initialization few times since it doesn't always succeed in
170 * first time.
171 */
172
173 for (n = 0; n < 4; n++)
174 {
175 /*
176 * Wake up the card by sending a 32 byte Crystal key to the key port.
177 */
178
179 for (i = 0; i < 32; i++)
180 CS_OUT(crystal_key[i]);
181
182 sleep(HZ / 10);
183
184 /*
185 * Now set the CSN (Card Select Number).
186 */
187
188 CS_OUT2(0x06, CSN_NUM);
189
190 /*
191 * Then set some config bytes. First logical device 0
192 */
193
194 CS_OUT2(0x15, 0x00); /* Select logical device 0 (WSS/SB/FM) */
195 CS_OUT3(0x47, (base >> 8) & 0xff, base & 0xff); /* WSS base */
196
197 if (!request_region(0x388, 4, "FM")) /* Not free */
198 CS_OUT3(0x48, 0x00, 0x00) /* FM base off */
199 else {
200 release_region(0x388, 4);
201 CS_OUT3(0x48, 0x03, 0x88); /* FM base 0x388 */
202 }
203
204 CS_OUT3(0x42, 0x00, 0x00); /* SB base off */
205 CS_OUT2(0x22, irq); /* SB+WSS IRQ */
206 CS_OUT2(0x2a, dma1); /* SB+WSS DMA */
207
208 if (dma2 != -1)
209 CS_OUT2(0x25, dma2) /* WSS DMA2 */
210 else
211 CS_OUT2(0x25, 4); /* No WSS DMA2 */
212
213 CS_OUT2(0x33, 0x01); /* Activate logical dev 0 */
214
215 sleep(HZ / 10);
216
217 /*
218 * Initialize logical device 3 (MPU)
219 */
220
221 if (mpu_base != 0 && mpu_irq != 0)
222 {
223 CS_OUT2(0x15, 0x03); /* Select logical device 3 (MPU) */
224 CS_OUT3(0x47, (mpu_base >> 8) & 0xff, mpu_base & 0xff); /* MPU base */
225 CS_OUT2(0x22, mpu_irq); /* MPU IRQ */
226 CS_OUT2(0x33, 0x01); /* Activate logical dev 3 */
227 }
228
229 if(synth_base != 0)
230 {
231 CS_OUT2 (0x15, 0x04); /* logical device 4 (WaveFront) */
232 CS_OUT3 (0x47, (synth_base >> 8) & 0xff,
233 synth_base & 0xff); /* base */
234 CS_OUT2 (0x22, synth_irq); /* IRQ */
235 CS_OUT2 (0x33, 0x01); /* Activate logical dev 4 */
236 }
237
238 /*
239 * Finally activate the chip
240 */
241
242 CS_OUT(0x79);
243
244 sleep(HZ / 5);
245
246 /*
247 * Then try to detect the codec part of the chip
248 */
249
250 if (ad1848_detect(ports, NULL, hw_config->osp))
251 goto got_it;
252
253 sleep(HZ);
254 }
255fail:
256 release_region(base, 4);
257 return 0;
258
259got_it:
260 if (dma2 == -1)
261 dma2 = dma1;
262
263 hw_config->slots[0] = ad1848_init("Crystal audio controller", ports,
264 irq,
265 dma1, /* Playback DMA */
266 dma2, /* Capture DMA */
267 0,
268 hw_config->osp,
269 THIS_MODULE);
270
271 if (hw_config->slots[0] != -1 &&
272 audio_devs[hw_config->slots[0]]->mixer_dev!=-1)
273 {
274 /* Assume the mixer map is as suggested in the CS4232 databook */
275 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
276 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
277 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_SYNTH); /* FM synth */
278 }
279 if (mpu_base != 0 && mpu_irq != 0)
280 {
281 static struct address_info hw_config2 = {
282 0
283 }; /* Ensure it's initialized */
284
285 hw_config2.io_base = mpu_base;
286 hw_config2.irq = mpu_irq;
287 hw_config2.dma = -1;
288 hw_config2.dma2 = -1;
289 hw_config2.always_detect = 0;
290 hw_config2.name = NULL;
291 hw_config2.driver_use_1 = 0;
292 hw_config2.driver_use_2 = 0;
293 hw_config2.card_subtype = 0;
294
295 if (probe_uart401(&hw_config2, THIS_MODULE))
296 {
297 mpu_detected = 1;
298 }
299 else
300 {
301 mpu_base = mpu_irq = 0;
302 }
303 hw_config->slots[1] = hw_config2.slots[1];
304 }
305
306 if (bss)
307 enable_xctrl(base);
308
309 return 1;
310}
311
312static void __devexit unload_cs4232(struct address_info *hw_config)
313{
314 int base = hw_config->io_base, irq = hw_config->irq;
315 int dma1 = hw_config->dma, dma2 = hw_config->dma2;
316
317 if (dma2 == -1)
318 dma2 = dma1;
319
320 ad1848_unload(base,
321 irq,
322 dma1, /* Playback DMA */
323 dma2, /* Capture DMA */
324 0);
325
326 sound_unload_audiodev(hw_config->slots[0]);
327 if (mpu_base != 0 && mpu_irq != 0 && mpu_detected)
328 {
329 static struct address_info hw_config2 =
330 {
331 0
332 }; /* Ensure it's initialized */
333
334 hw_config2.io_base = mpu_base;
335 hw_config2.irq = mpu_irq;
336 hw_config2.dma = -1;
337 hw_config2.dma2 = -1;
338 hw_config2.always_detect = 0;
339 hw_config2.name = NULL;
340 hw_config2.driver_use_1 = 0;
341 hw_config2.driver_use_2 = 0;
342 hw_config2.card_subtype = 0;
343 hw_config2.slots[1] = hw_config->slots[1];
344
345 unload_uart401(&hw_config2);
346 }
347}
348
349static struct address_info cfg;
350static struct address_info cfg_mpu;
351
352static int __initdata io = -1;
353static int __initdata irq = -1;
354static int __initdata dma = -1;
355static int __initdata dma2 = -1;
356static int __initdata mpuio = -1;
357static int __initdata mpuirq = -1;
358static int __initdata synthio = -1;
359static int __initdata synthirq = -1;
360static int __initdata isapnp = 1;
361
362static unsigned int cs4232_devices;
363
364MODULE_DESCRIPTION("CS4232 based soundcard driver");
365MODULE_AUTHOR("Hannu Savolainen, Paul Barton-Davis");
366MODULE_LICENSE("GPL");
367
368module_param(io, int, 0);
369MODULE_PARM_DESC(io,"base I/O port for AD1848");
370module_param(irq, int, 0);
371MODULE_PARM_DESC(irq,"IRQ for AD1848 chip");
372module_param(dma, int, 0);
373MODULE_PARM_DESC(dma,"8 bit DMA for AD1848 chip");
374module_param(dma2, int, 0);
375MODULE_PARM_DESC(dma2,"16 bit DMA for AD1848 chip");
376module_param(mpuio, int, 0);
377MODULE_PARM_DESC(mpuio,"MPU 401 base address");
378module_param(mpuirq, int, 0);
379MODULE_PARM_DESC(mpuirq,"MPU 401 IRQ");
380module_param(synthio, int, 0);
381MODULE_PARM_DESC(synthio,"Maui WaveTable base I/O port");
382module_param(synthirq, int, 0);
383MODULE_PARM_DESC(synthirq,"Maui WaveTable IRQ");
384module_param(isapnp, bool, 0);
385MODULE_PARM_DESC(isapnp,"Enable ISAPnP probing (default 1)");
386module_param(bss, bool, 0);
387MODULE_PARM_DESC(bss,"Enable Bose Sound System Support (default 0)");
388
389/*
390 * Install a CS4232 based card. Need to have ad1848 and mpu401
391 * loaded ready.
392 */
393
394/* All cs4232 based cards have the main ad1848 card either as CSC0000 or
395 * CSC0100. */
396static const struct pnp_device_id cs4232_pnp_table[] = {
397 { .id = "CSC0100", .driver_data = 0 },
398 { .id = "CSC0000", .driver_data = 0 },
399 /* Guillemot Turtlebeach something appears to be cs4232 compatible
400 * (untested) */
401 { .id = "GIM0100", .driver_data = 0 },
402 { .id = ""}
403};
404
405MODULE_DEVICE_TABLE(pnp, cs4232_pnp_table);
406
407static int __init cs4232_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
408{
409 struct address_info *isapnpcfg;
410
411 isapnpcfg = kmalloc(sizeof(*isapnpcfg),GFP_KERNEL);
412 if (!isapnpcfg)
413 return -ENOMEM;
414
415 isapnpcfg->irq = pnp_irq(dev, 0);
416 isapnpcfg->dma = pnp_dma(dev, 0);
417 isapnpcfg->dma2 = pnp_dma(dev, 1);
418 isapnpcfg->io_base = pnp_port_start(dev, 0);
419 if (probe_cs4232(isapnpcfg,TRUE) == 0) {
420 printk(KERN_ERR "cs4232: ISA PnP card found, but not detected?\n");
421 kfree(isapnpcfg);
422 return -ENODEV;
423 }
424 pnp_set_drvdata(dev,isapnpcfg);
425 cs4232_devices++;
426 return 0;
427}
428
429static void __devexit cs4232_pnp_remove(struct pnp_dev *dev)
430{
431 struct address_info *cfg = pnp_get_drvdata(dev);
432 if (cfg) {
433 unload_cs4232(cfg);
434 kfree(cfg);
435 }
436}
437
438static struct pnp_driver cs4232_driver = {
439 .name = "cs4232",
440 .id_table = cs4232_pnp_table,
441 .probe = cs4232_pnp_probe,
442 .remove = __devexit_p(cs4232_pnp_remove),
443};
444
445static int __init init_cs4232(void)
446{
447#ifdef CONFIG_SOUND_WAVEFRONT_MODULE
448 if(synthio == -1)
449 printk(KERN_INFO "cs4232: set synthio and synthirq to use the wavefront facilities.\n");
450 else {
451 synth_base = synthio;
452 synth_irq = synthirq;
453 }
454#else
455 if(synthio != -1)
456 printk(KERN_WARNING "cs4232: wavefront support not enabled in this driver.\n");
457#endif
458 cfg.irq = -1;
459
460 if (isapnp) {
461 pnp_register_driver(&cs4232_driver);
462 if (cs4232_devices)
463 return 0;
464 }
465
466 if(io==-1||irq==-1||dma==-1)
467 {
468 printk(KERN_ERR "cs4232: Must set io, irq and dma.\n");
469 return -ENODEV;
470 }
471
472 cfg.io_base = io;
473 cfg.irq = irq;
474 cfg.dma = dma;
475 cfg.dma2 = dma2;
476
477 cfg_mpu.io_base = -1;
478 cfg_mpu.irq = -1;
479
480 if (mpuio != -1 && mpuirq != -1) {
481 cfg_mpu.io_base = mpuio;
482 cfg_mpu.irq = mpuirq;
483 probe_cs4232_mpu(&cfg_mpu); /* Bug always returns 0 not OK -- AC */
484 }
485
486 if (probe_cs4232(&cfg,FALSE) == 0)
487 return -ENODEV;
488
489 return 0;
490}
491
492static void __exit cleanup_cs4232(void)
493{
494 pnp_unregister_driver(&cs4232_driver);
495 if (cfg.irq != -1)
496 unload_cs4232(&cfg); /* Unloads global MPU as well, if needed */
497}
498
499module_init(init_cs4232);
500module_exit(cleanup_cs4232);
501
502#ifndef MODULE
503static int __init setup_cs4232(char *str)
504{
505 /* io, irq, dma, dma2 mpuio, mpuirq*/
506 int ints[7];
507
508 /* If we have isapnp cards, no need for options */
509 pnp_register_driver(&cs4232_driver);
510 if (cs4232_devices)
511 return 1;
512
513 str = get_options(str, ARRAY_SIZE(ints), ints);
514
515 io = ints[1];
516 irq = ints[2];
517 dma = ints[3];
518 dma2 = ints[4];
519 mpuio = ints[5];
520 mpuirq = ints[6];
521
522 return 1;
523}
524
525__setup("cs4232=", setup_cs4232);
526#endif
diff --git a/sound/oss/i810_audio.c b/sound/oss/i810_audio.c
deleted file mode 100644
index f5e31f11973d..000000000000
--- a/sound/oss/i810_audio.c
+++ /dev/null
@@ -1,3656 +0,0 @@
1/*
2 * Intel i810 and friends ICH driver for Linux
3 * Alan Cox <alan@redhat.com>
4 *
5 * Built from:
6 * Low level code: Zach Brown (original nonworking i810 OSS driver)
7 * Jaroslav Kysela <perex@suse.cz> (working ALSA driver)
8 *
9 * Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
10 * Extended by: Zach Brown <zab@redhat.com>
11 * and others..
12 *
13 * Hardware Provided By:
14 * Analog Devices (A major AC97 codec maker)
15 * Intel Corp (you've probably heard of them already)
16 *
17 * AC97 clues and assistance provided by
18 * Analog Devices
19 * Zach 'Fufu' Brown
20 * Jeff Garzik
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *
36 *
37 * Intel 810 theory of operation
38 *
39 * The chipset provides three DMA channels that talk to an AC97
40 * CODEC (AC97 is a digital/analog mixer standard). At its simplest
41 * you get 48Khz audio with basic volume and mixer controls. At the
42 * best you get rate adaption in the codec. We set the card up so
43 * that we never take completion interrupts but instead keep the card
44 * chasing its tail around a ring buffer. This is needed for mmap
45 * mode audio and happens to work rather well for non-mmap modes too.
46 *
47 * The board has one output channel for PCM audio (supported) and
48 * a stereo line in and mono microphone input. Again these are normally
49 * locked to 48Khz only. Right now recording is not finished.
50 *
51 * There is no midi support, no synth support. Use timidity. To get
52 * esd working you need to use esd -r 48000 as it won't probe 48KHz
53 * by default. mpg123 can't handle 48Khz only audio so use xmms.
54 *
55 * Fix The Sound On Dell
56 *
57 * Not everyone uses 48KHz. We know of no way to detect this reliably
58 * and certainly not to get the right data. If your i810 audio sounds
59 * stupid you may need to investigate other speeds. According to Analog
60 * they tend to use a 14.318MHz clock which gives you a base rate of
61 * 41194Hz.
62 *
63 * This is available via the 'ftsodell=1' option.
64 *
65 * If you need to force a specific rate set the clocking= option
66 *
67 * This driver is cursed. (Ben LaHaise)
68 *
69 * ICH 3 caveats
70 * Intel errata #7 for ICH3 IO. We need to disable SMI stuff
71 * when codec probing. [Not Yet Done]
72 *
73 * ICH 4 caveats
74 *
75 * The ICH4 has the feature, that the codec ID doesn't have to be
76 * congruent with the IO connection.
77 *
78 * Therefore, from driver version 0.23 on, there is a "codec ID" <->
79 * "IO register base offset" mapping (card->ac97_id_map) field.
80 *
81 * Juergen "George" Sawinski (jsaw)
82 */
83
84#include <linux/module.h>
85#include <linux/string.h>
86#include <linux/ctype.h>
87#include <linux/ioport.h>
88#include <linux/sched.h>
89#include <linux/delay.h>
90#include <linux/sound.h>
91#include <linux/slab.h>
92#include <linux/soundcard.h>
93#include <linux/pci.h>
94#include <linux/interrupt.h>
95#include <asm/io.h>
96#include <asm/dma.h>
97#include <linux/init.h>
98#include <linux/poll.h>
99#include <linux/spinlock.h>
100#include <linux/smp_lock.h>
101#include <linux/ac97_codec.h>
102#include <linux/bitops.h>
103#include <linux/mutex.h>
104#include <linux/mm.h>
105
106#include <asm/uaccess.h>
107
108#define DRIVER_VERSION "1.01"
109
110#define MODULOP2(a, b) ((a) & ((b) - 1))
111#define MASKP2(a, b) ((a) & ~((b) - 1))
112
113static int ftsodell;
114static int strict_clocking;
115static unsigned int clocking;
116static int spdif_locked;
117static int ac97_quirk = AC97_TUNE_DEFAULT;
118
119//#define DEBUG
120//#define DEBUG2
121//#define DEBUG_INTERRUPTS
122//#define DEBUG_MMAP
123//#define DEBUG_MMIO
124
125#define ADC_RUNNING 1
126#define DAC_RUNNING 2
127
128#define I810_FMT_16BIT 1
129#define I810_FMT_STEREO 2
130#define I810_FMT_MASK 3
131
132#define SPDIF_ON 0x0004
133#define SURR_ON 0x0010
134#define CENTER_LFE_ON 0x0020
135#define VOL_MUTED 0x8000
136
137/* the 810's array of pointers to data buffers */
138
139struct sg_item {
140#define BUSADDR_MASK 0xFFFFFFFE
141 u32 busaddr;
142#define CON_IOC 0x80000000 /* interrupt on completion */
143#define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
144#define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
145 u32 control;
146};
147
148/* an instance of the i810 channel */
149#define SG_LEN 32
150struct i810_channel
151{
152 /* these sg guys should probably be allocated
153 separately as nocache. Must be 8 byte aligned */
154 struct sg_item sg[SG_LEN]; /* 32*8 */
155 u32 offset; /* 4 */
156 u32 port; /* 4 */
157 u32 used;
158 u32 num;
159};
160
161/*
162 * we have 3 separate dma engines. pcm in, pcm out, and mic.
163 * each dma engine has controlling registers. These goofy
164 * names are from the datasheet, but make it easy to write
165 * code while leafing through it.
166 *
167 * ICH4 has 6 dma engines, pcm in, pcm out, mic, pcm in 2,
168 * mic in 2, s/pdif. Of special interest is the fact that
169 * the upper 3 DMA engines on the ICH4 *must* be accessed
170 * via mmio access instead of pio access.
171 */
172
173#define ENUM_ENGINE(PRE,DIG) \
174enum { \
175 PRE##_BASE = 0x##DIG##0, /* Base Address */ \
176 PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */ \
177 PRE##_CIV = 0x##DIG##4, /* Current Index Value */ \
178 PRE##_LVI = 0x##DIG##5, /* Last Valid Index */ \
179 PRE##_SR = 0x##DIG##6, /* Status Register */ \
180 PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */ \
181 PRE##_PIV = 0x##DIG##a, /* Prefetched Index Value */ \
182 PRE##_CR = 0x##DIG##b /* Control Register */ \
183}
184
185ENUM_ENGINE(OFF,0); /* Offsets */
186ENUM_ENGINE(PI,0); /* PCM In */
187ENUM_ENGINE(PO,1); /* PCM Out */
188ENUM_ENGINE(MC,2); /* Mic In */
189
190enum {
191 GLOB_CNT = 0x2c, /* Global Control */
192 GLOB_STA = 0x30, /* Global Status */
193 CAS = 0x34 /* Codec Write Semaphore Register */
194};
195
196ENUM_ENGINE(MC2,4); /* Mic In 2 */
197ENUM_ENGINE(PI2,5); /* PCM In 2 */
198ENUM_ENGINE(SP,6); /* S/PDIF */
199
200enum {
201 SDM = 0x80 /* SDATA_IN Map Register */
202};
203
204/* interrupts for a dma engine */
205#define DMA_INT_FIFO (1<<4) /* fifo under/over flow */
206#define DMA_INT_COMPLETE (1<<3) /* buffer read/write complete and ioc set */
207#define DMA_INT_LVI (1<<2) /* last valid done */
208#define DMA_INT_CELV (1<<1) /* last valid is current */
209#define DMA_INT_DCH (1) /* DMA Controller Halted (happens on LVI interrupts) */
210#define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
211
212/* interrupts for the whole chip */
213#define INT_SEC (1<<11)
214#define INT_PRI (1<<10)
215#define INT_MC (1<<7)
216#define INT_PO (1<<6)
217#define INT_PI (1<<5)
218#define INT_MO (1<<2)
219#define INT_NI (1<<1)
220#define INT_GPI (1<<0)
221#define INT_MASK (INT_SEC|INT_PRI|INT_MC|INT_PO|INT_PI|INT_MO|INT_NI|INT_GPI)
222
223/* magic numbers to protect our data structures */
224#define I810_CARD_MAGIC 0x5072696E /* "Prin" */
225#define I810_STATE_MAGIC 0x63657373 /* "cess" */
226#define I810_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
227#define NR_HW_CH 3
228
229/* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
230#define NR_AC97 4
231
232/* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
233/* stream at a minimum for this card to be happy */
234static const unsigned sample_size[] = { 1, 2, 2, 4 };
235/* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
236/* values are one less than might be expected */
237static const unsigned sample_shift[] = { -1, 0, 0, 1 };
238
239enum {
240 ICH82801AA = 0,
241 ICH82901AB,
242 INTEL440MX,
243 INTELICH2,
244 INTELICH3,
245 INTELICH4,
246 INTELICH5,
247 SI7012,
248 NVIDIA_NFORCE,
249 AMD768,
250 AMD8111
251};
252
253static char * card_names[] = {
254 "Intel ICH 82801AA",
255 "Intel ICH 82901AB",
256 "Intel 440MX",
257 "Intel ICH2",
258 "Intel ICH3",
259 "Intel ICH4",
260 "Intel ICH5",
261 "SiS 7012",
262 "NVIDIA nForce Audio",
263 "AMD 768",
264 "AMD-8111 IOHub"
265};
266
267/* These are capabilities (and bugs) the chipsets _can_ have */
268static struct {
269 int16_t nr_ac97;
270#define CAP_MMIO 0x0001
271#define CAP_20BIT_AUDIO_SUPPORT 0x0002
272 u_int16_t flags;
273} card_cap[] = {
274 { 1, 0x0000 }, /* ICH82801AA */
275 { 1, 0x0000 }, /* ICH82901AB */
276 { 1, 0x0000 }, /* INTEL440MX */
277 { 1, 0x0000 }, /* INTELICH2 */
278 { 2, 0x0000 }, /* INTELICH3 */
279 { 3, 0x0003 }, /* INTELICH4 */
280 { 3, 0x0003 }, /* INTELICH5 */
281 /*@FIXME to be verified*/ { 2, 0x0000 }, /* SI7012 */
282 /*@FIXME to be verified*/ { 2, 0x0000 }, /* NVIDIA_NFORCE */
283 /*@FIXME to be verified*/ { 2, 0x0000 }, /* AMD768 */
284 /*@FIXME to be verified*/ { 3, 0x0001 }, /* AMD8111 */
285};
286
287static struct pci_device_id i810_pci_tbl [] = {
288 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_5,
289 PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82801AA},
290 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_5,
291 PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82901AB},
292 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_440MX,
293 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTEL440MX},
294 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_4,
295 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH2},
296 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_5,
297 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH3},
298 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_5,
299 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
300 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_5,
301 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH5},
302 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7012,
303 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SI7012},
304 {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO,
305 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
306 {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO,
307 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
308 {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO,
309 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
310 {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7445,
311 PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD768},
312 {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_AUDIO,
313 PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD8111},
314 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_5,
315 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
316 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_18,
317 PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
318 {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_AUDIO,
319 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
320 {0,}
321};
322
323MODULE_DEVICE_TABLE (pci, i810_pci_tbl);
324
325#ifdef CONFIG_PM
326#define PM_SUSPENDED(card) (card->pm_suspended)
327#else
328#define PM_SUSPENDED(card) (0)
329#endif
330
331/* "software" or virtual channel, an instance of opened /dev/dsp */
332struct i810_state {
333 unsigned int magic;
334 struct i810_card *card; /* Card info */
335
336 /* single open lock mechanism, only used for recording */
337 struct mutex open_mutex;
338 wait_queue_head_t open_wait;
339
340 /* file mode */
341 mode_t open_mode;
342
343 /* virtual channel number */
344 int virt;
345
346#ifdef CONFIG_PM
347 unsigned int pm_saved_dac_rate,pm_saved_adc_rate;
348#endif
349 struct dmabuf {
350 /* wave sample stuff */
351 unsigned int rate;
352 unsigned char fmt, enable, trigger;
353
354 /* hardware channel */
355 struct i810_channel *read_channel;
356 struct i810_channel *write_channel;
357
358 /* OSS buffer management stuff */
359 void *rawbuf;
360 dma_addr_t dma_handle;
361 unsigned buforder;
362 unsigned numfrag;
363 unsigned fragshift;
364
365 /* our buffer acts like a circular ring */
366 unsigned hwptr; /* where dma last started, updated by update_ptr */
367 unsigned swptr; /* where driver last clear/filled, updated by read/write */
368 int count; /* bytes to be consumed or been generated by dma machine */
369 unsigned total_bytes; /* total bytes dmaed by hardware */
370
371 unsigned error; /* number of over/underruns */
372 wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
373
374 /* redundant, but makes calculations easier */
375 /* what the hardware uses */
376 unsigned dmasize;
377 unsigned fragsize;
378 unsigned fragsamples;
379
380 /* what we tell the user to expect */
381 unsigned userfrags;
382 unsigned userfragsize;
383
384 /* OSS stuff */
385 unsigned mapped:1;
386 unsigned ready:1;
387 unsigned update_flag;
388 unsigned ossfragsize;
389 unsigned ossmaxfrags;
390 unsigned subdivision;
391 } dmabuf;
392};
393
394
395struct i810_card {
396 unsigned int magic;
397
398 /* We keep i810 cards in a linked list */
399 struct i810_card *next;
400
401 /* The i810 has a certain amount of cross channel interaction
402 so we use a single per card lock */
403 spinlock_t lock;
404
405 /* Control AC97 access serialization */
406 spinlock_t ac97_lock;
407
408 /* PCI device stuff */
409 struct pci_dev * pci_dev;
410 u16 pci_id;
411 u16 pci_id_internal; /* used to access card_cap[] */
412#ifdef CONFIG_PM
413 u16 pm_suspended;
414 int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
415#endif
416 /* soundcore stuff */
417 int dev_audio;
418
419 /* structures for abstraction of hardware facilities, codecs, banks and channels*/
420 u16 ac97_id_map[NR_AC97];
421 struct ac97_codec *ac97_codec[NR_AC97];
422 struct i810_state *states[NR_HW_CH];
423 struct i810_channel *channel; /* 1:1 to states[] but diff. lifetime */
424 dma_addr_t chandma;
425
426 u16 ac97_features;
427 u16 ac97_status;
428 u16 channels;
429
430 /* hardware resources */
431 unsigned long ac97base;
432 unsigned long iobase;
433 u32 irq;
434
435 unsigned long ac97base_mmio_phys;
436 unsigned long iobase_mmio_phys;
437 u_int8_t __iomem *ac97base_mmio;
438 u_int8_t __iomem *iobase_mmio;
439
440 int use_mmio;
441
442 /* Function support */
443 struct i810_channel *(*alloc_pcm_channel)(struct i810_card *);
444 struct i810_channel *(*alloc_rec_pcm_channel)(struct i810_card *);
445 struct i810_channel *(*alloc_rec_mic_channel)(struct i810_card *);
446 void (*free_pcm_channel)(struct i810_card *, int chan);
447
448 /* We have a *very* long init time possibly, so use this to block */
449 /* attempts to open our devices before we are ready (stops oops'es) */
450 int initializing;
451};
452
453/* extract register offset from codec struct */
454#define IO_REG_OFF(codec) (((struct i810_card *) codec->private_data)->ac97_id_map[codec->id])
455
456#define I810_IOREAD(size, type, card, off) \
457({ \
458 type val; \
459 if (card->use_mmio) \
460 val=read##size(card->iobase_mmio+off); \
461 else \
462 val=in##size(card->iobase+off); \
463 val; \
464})
465
466#define I810_IOREADL(card, off) I810_IOREAD(l, u32, card, off)
467#define I810_IOREADW(card, off) I810_IOREAD(w, u16, card, off)
468#define I810_IOREADB(card, off) I810_IOREAD(b, u8, card, off)
469
470#define I810_IOWRITE(size, val, card, off) \
471({ \
472 if (card->use_mmio) \
473 write##size(val, card->iobase_mmio+off); \
474 else \
475 out##size(val, card->iobase+off); \
476})
477
478#define I810_IOWRITEL(val, card, off) I810_IOWRITE(l, val, card, off)
479#define I810_IOWRITEW(val, card, off) I810_IOWRITE(w, val, card, off)
480#define I810_IOWRITEB(val, card, off) I810_IOWRITE(b, val, card, off)
481
482#define GET_CIV(card, port) MODULOP2(I810_IOREADB((card), (port) + OFF_CIV), SG_LEN)
483#define GET_LVI(card, port) MODULOP2(I810_IOREADB((card), (port) + OFF_LVI), SG_LEN)
484
485/* set LVI from CIV */
486#define CIV_TO_LVI(card, port, off) \
487 I810_IOWRITEB(MODULOP2(GET_CIV((card), (port)) + (off), SG_LEN), (card), (port) + OFF_LVI)
488
489static struct ac97_quirk ac97_quirks[] __devinitdata = {
490 {
491 .vendor = 0x0e11,
492 .device = 0x00b8,
493 .name = "Compaq Evo D510C",
494 .type = AC97_TUNE_HP_ONLY
495 },
496 {
497 .vendor = 0x1028,
498 .device = 0x00d8,
499 .name = "Dell Precision 530", /* AD1885 */
500 .type = AC97_TUNE_HP_ONLY
501 },
502 {
503 .vendor = 0x1028,
504 .device = 0x0126,
505 .name = "Dell Optiplex GX260", /* AD1981A */
506 .type = AC97_TUNE_HP_ONLY
507 },
508 {
509 .vendor = 0x1028,
510 .device = 0x012d,
511 .name = "Dell Precision 450", /* AD1981B*/
512 .type = AC97_TUNE_HP_ONLY
513 },
514 { /* FIXME: which codec? */
515 .vendor = 0x103c,
516 .device = 0x00c3,
517 .name = "Hewlett-Packard onboard",
518 .type = AC97_TUNE_HP_ONLY
519 },
520 {
521 .vendor = 0x103c,
522 .device = 0x12f1,
523 .name = "HP xw8200", /* AD1981B*/
524 .type = AC97_TUNE_HP_ONLY
525 },
526 {
527 .vendor = 0x103c,
528 .device = 0x3008,
529 .name = "HP xw4200", /* AD1981B*/
530 .type = AC97_TUNE_HP_ONLY
531 },
532 {
533 .vendor = 0x10f1,
534 .device = 0x2665,
535 .name = "Fujitsu-Siemens Celsius", /* AD1981? */
536 .type = AC97_TUNE_HP_ONLY
537 },
538 {
539 .vendor = 0x10f1,
540 .device = 0x2885,
541 .name = "AMD64 Mobo", /* ALC650 */
542 .type = AC97_TUNE_HP_ONLY
543 },
544 {
545 .vendor = 0x110a,
546 .device = 0x0056,
547 .name = "Fujitsu-Siemens Scenic", /* AD1981? */
548 .type = AC97_TUNE_HP_ONLY
549 },
550 {
551 .vendor = 0x11d4,
552 .device = 0x5375,
553 .name = "ADI AD1985 (discrete)",
554 .type = AC97_TUNE_HP_ONLY
555 },
556 {
557 .vendor = 0x1462,
558 .device = 0x5470,
559 .name = "MSI P4 ATX 645 Ultra",
560 .type = AC97_TUNE_HP_ONLY
561 },
562 {
563 .vendor = 0x1734,
564 .device = 0x0088,
565 .name = "Fujitsu-Siemens D1522", /* AD1981 */
566 .type = AC97_TUNE_HP_ONLY
567 },
568 {
569 .vendor = 0x8086,
570 .device = 0x4856,
571 .name = "Intel D845WN (82801BA)",
572 .type = AC97_TUNE_SWAP_HP
573 },
574 {
575 .vendor = 0x8086,
576 .device = 0x4d44,
577 .name = "Intel D850EMV2", /* AD1885 */
578 .type = AC97_TUNE_HP_ONLY
579 },
580 {
581 .vendor = 0x8086,
582 .device = 0x4d56,
583 .name = "Intel ICH/AD1885",
584 .type = AC97_TUNE_HP_ONLY
585 },
586 {
587 .vendor = 0x1028,
588 .device = 0x012d,
589 .name = "Dell Precision 450", /* AD1981B*/
590 .type = AC97_TUNE_HP_ONLY
591 },
592 {
593 .vendor = 0x103c,
594 .device = 0x3008,
595 .name = "HP xw4200", /* AD1981B*/
596 .type = AC97_TUNE_HP_ONLY
597 },
598 {
599 .vendor = 0x103c,
600 .device = 0x12f1,
601 .name = "HP xw8200", /* AD1981B*/
602 .type = AC97_TUNE_HP_ONLY
603 },
604 { } /* terminator */
605};
606
607static struct i810_card *devs = NULL;
608
609static int i810_open_mixdev(struct inode *inode, struct file *file);
610static int i810_ioctl_mixdev(struct inode *inode, struct file *file,
611 unsigned int cmd, unsigned long arg);
612static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg);
613static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
614static u16 i810_ac97_get_mmio(struct ac97_codec *dev, u8 reg);
615static void i810_ac97_set_mmio(struct ac97_codec *dev, u8 reg, u16 data);
616static u16 i810_ac97_get_io(struct ac97_codec *dev, u8 reg);
617static void i810_ac97_set_io(struct ac97_codec *dev, u8 reg, u16 data);
618
619static struct i810_channel *i810_alloc_pcm_channel(struct i810_card *card)
620{
621 if(card->channel[1].used==1)
622 return NULL;
623 card->channel[1].used=1;
624 return &card->channel[1];
625}
626
627static struct i810_channel *i810_alloc_rec_pcm_channel(struct i810_card *card)
628{
629 if(card->channel[0].used==1)
630 return NULL;
631 card->channel[0].used=1;
632 return &card->channel[0];
633}
634
635static struct i810_channel *i810_alloc_rec_mic_channel(struct i810_card *card)
636{
637 if(card->channel[2].used==1)
638 return NULL;
639 card->channel[2].used=1;
640 return &card->channel[2];
641}
642
643static void i810_free_pcm_channel(struct i810_card *card, int channel)
644{
645 card->channel[channel].used=0;
646}
647
648static int i810_valid_spdif_rate ( struct ac97_codec *codec, int rate )
649{
650 unsigned long id = 0L;
651
652 id = (i810_ac97_get(codec, AC97_VENDOR_ID1) << 16);
653 id |= i810_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
654#ifdef DEBUG
655 printk ( "i810_audio: codec = %s, codec_id = 0x%08lx\n", codec->name, id);
656#endif
657 switch ( id ) {
658 case 0x41445361: /* AD1886 */
659 if (rate == 48000) {
660 return 1;
661 }
662 break;
663 default: /* all other codecs, until we know otherwiae */
664 if (rate == 48000 || rate == 44100 || rate == 32000) {
665 return 1;
666 }
667 break;
668 }
669 return (0);
670}
671
672/* i810_set_spdif_output
673 *
674 * Configure the S/PDIF output transmitter. When we turn on
675 * S/PDIF, we turn off the analog output. This may not be
676 * the right thing to do.
677 *
678 * Assumptions:
679 * The DSP sample rate must already be set to a supported
680 * S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
681 */
682static int i810_set_spdif_output(struct i810_state *state, int slots, int rate)
683{
684 int vol;
685 int aud_reg;
686 int r = 0;
687 struct ac97_codec *codec = state->card->ac97_codec[0];
688
689 if(!codec->codec_ops->digital) {
690 state->card->ac97_status &= ~SPDIF_ON;
691 } else {
692 if ( slots == -1 ) { /* Turn off S/PDIF */
693 codec->codec_ops->digital(codec, 0, 0, 0);
694 /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
695 if ( !(state->card->ac97_status & VOL_MUTED) ) {
696 aud_reg = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
697 i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (aud_reg & ~VOL_MUTED));
698 }
699 state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
700 return 0;
701 }
702
703 vol = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
704 state->card->ac97_status = vol & VOL_MUTED;
705
706 r = codec->codec_ops->digital(codec, slots, rate, 0);
707
708 if(r)
709 state->card->ac97_status |= SPDIF_ON;
710 else
711 state->card->ac97_status &= ~SPDIF_ON;
712
713 /* Mute the analog output */
714 /* Should this only mute the PCM volume??? */
715 i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (vol | VOL_MUTED));
716 }
717 return r;
718}
719
720/* i810_set_dac_channels
721 *
722 * Configure the codec's multi-channel DACs
723 *
724 * The logic is backwards. Setting the bit to 1 turns off the DAC.
725 *
726 * What about the ICH? We currently configure it using the
727 * SNDCTL_DSP_CHANNELS ioctl. If we're turnning on the DAC,
728 * does that imply that we want the ICH set to support
729 * these channels?
730 *
731 * TODO:
732 * vailidate that the codec really supports these DACs
733 * before turning them on.
734 */
735static void i810_set_dac_channels(struct i810_state *state, int channel)
736{
737 int aud_reg;
738 struct ac97_codec *codec = state->card->ac97_codec[0];
739
740 /* No codec, no setup */
741
742 if(codec == NULL)
743 return;
744
745 aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
746 aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
747 state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
748
749 switch ( channel ) {
750 case 2: /* always enabled */
751 break;
752 case 4:
753 aud_reg &= ~AC97_EA_PRJ;
754 state->card->ac97_status |= SURR_ON;
755 break;
756 case 6:
757 aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
758 state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
759 break;
760 default:
761 break;
762 }
763 i810_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
764
765}
766
767
768/* set playback sample rate */
769static unsigned int i810_set_dac_rate(struct i810_state * state, unsigned int rate)
770{
771 struct dmabuf *dmabuf = &state->dmabuf;
772 u32 new_rate;
773 struct ac97_codec *codec=state->card->ac97_codec[0];
774
775 if(!(state->card->ac97_features&0x0001))
776 {
777 dmabuf->rate = clocking;
778#ifdef DEBUG
779 printk("Asked for %d Hz, but ac97_features says we only do %dHz. Sorry!\n",
780 rate,clocking);
781#endif
782 return clocking;
783 }
784
785 if (rate > 48000)
786 rate = 48000;
787 if (rate < 8000)
788 rate = 8000;
789 dmabuf->rate = rate;
790
791 /*
792 * Adjust for misclocked crap
793 */
794 rate = ( rate * clocking)/48000;
795 if(strict_clocking && rate < 8000) {
796 rate = 8000;
797 dmabuf->rate = (rate * 48000)/clocking;
798 }
799
800 new_rate=ac97_set_dac_rate(codec, rate);
801 if(new_rate != rate) {
802 dmabuf->rate = (new_rate * 48000)/clocking;
803 }
804#ifdef DEBUG
805 printk("i810_audio: called i810_set_dac_rate : asked for %d, got %d\n", rate, dmabuf->rate);
806#endif
807 rate = new_rate;
808 return dmabuf->rate;
809}
810
811/* set recording sample rate */
812static unsigned int i810_set_adc_rate(struct i810_state * state, unsigned int rate)
813{
814 struct dmabuf *dmabuf = &state->dmabuf;
815 u32 new_rate;
816 struct ac97_codec *codec=state->card->ac97_codec[0];
817
818 if(!(state->card->ac97_features&0x0001))
819 {
820 dmabuf->rate = clocking;
821 return clocking;
822 }
823
824 if (rate > 48000)
825 rate = 48000;
826 if (rate < 8000)
827 rate = 8000;
828 dmabuf->rate = rate;
829
830 /*
831 * Adjust for misclocked crap
832 */
833
834 rate = ( rate * clocking)/48000;
835 if(strict_clocking && rate < 8000) {
836 rate = 8000;
837 dmabuf->rate = (rate * 48000)/clocking;
838 }
839
840 new_rate = ac97_set_adc_rate(codec, rate);
841
842 if(new_rate != rate) {
843 dmabuf->rate = (new_rate * 48000)/clocking;
844 rate = new_rate;
845 }
846#ifdef DEBUG
847 printk("i810_audio: called i810_set_adc_rate : rate = %d/%d\n", dmabuf->rate, rate);
848#endif
849 return dmabuf->rate;
850}
851
852/* get current playback/recording dma buffer pointer (byte offset from LBA),
853 called with spinlock held! */
854
855static inline unsigned i810_get_dma_addr(struct i810_state *state, int rec)
856{
857 struct dmabuf *dmabuf = &state->dmabuf;
858 unsigned int civ, offset, port, port_picb, bytes = 2;
859
860 if (!dmabuf->enable)
861 return 0;
862
863 if (rec)
864 port = dmabuf->read_channel->port;
865 else
866 port = dmabuf->write_channel->port;
867
868 if(state->card->pci_id == PCI_DEVICE_ID_SI_7012) {
869 port_picb = port + OFF_SR;
870 bytes = 1;
871 } else
872 port_picb = port + OFF_PICB;
873
874 do {
875 civ = GET_CIV(state->card, port);
876 offset = I810_IOREADW(state->card, port_picb);
877 /* Must have a delay here! */
878 if(offset == 0)
879 udelay(1);
880 /* Reread both registers and make sure that that total
881 * offset from the first reading to the second is 0.
882 * There is an issue with SiS hardware where it will count
883 * picb down to 0, then update civ to the next value,
884 * then set the new picb to fragsize bytes. We can catch
885 * it between the civ update and the picb update, making
886 * it look as though we are 1 fragsize ahead of where we
887 * are. The next to we get the address though, it will
888 * be back in the right place, and we will suddenly think
889 * we just went forward dmasize - fragsize bytes, causing
890 * totally stupid *huge* dma overrun messages. We are
891 * assuming that the 1us delay is more than long enough
892 * that we won't have to worry about the chip still being
893 * out of sync with reality ;-)
894 */
895 } while (civ != GET_CIV(state->card, port) || offset != I810_IOREADW(state->card, port_picb));
896
897 return (((civ + 1) * dmabuf->fragsize - (bytes * offset))
898 % dmabuf->dmasize);
899}
900
901/* Stop recording (lock held) */
902static inline void __stop_adc(struct i810_state *state)
903{
904 struct dmabuf *dmabuf = &state->dmabuf;
905 struct i810_card *card = state->card;
906
907 dmabuf->enable &= ~ADC_RUNNING;
908 I810_IOWRITEB(0, card, PI_CR);
909 // wait for the card to acknowledge shutdown
910 while( I810_IOREADB(card, PI_CR) != 0 ) ;
911 // now clear any latent interrupt bits (like the halt bit)
912 if(card->pci_id == PCI_DEVICE_ID_SI_7012)
913 I810_IOWRITEB( I810_IOREADB(card, PI_PICB), card, PI_PICB );
914 else
915 I810_IOWRITEB( I810_IOREADB(card, PI_SR), card, PI_SR );
916 I810_IOWRITEL( I810_IOREADL(card, GLOB_STA) & INT_PI, card, GLOB_STA);
917}
918
919static void stop_adc(struct i810_state *state)
920{
921 struct i810_card *card = state->card;
922 unsigned long flags;
923
924 spin_lock_irqsave(&card->lock, flags);
925 __stop_adc(state);
926 spin_unlock_irqrestore(&card->lock, flags);
927}
928
929static inline void __start_adc(struct i810_state *state)
930{
931 struct dmabuf *dmabuf = &state->dmabuf;
932
933 if (dmabuf->count < dmabuf->dmasize && dmabuf->ready && !dmabuf->enable &&
934 (dmabuf->trigger & PCM_ENABLE_INPUT)) {
935 dmabuf->enable |= ADC_RUNNING;
936 // Interrupt enable, LVI enable, DMA enable
937 I810_IOWRITEB(0x10 | 0x04 | 0x01, state->card, PI_CR);
938 }
939}
940
941static void start_adc(struct i810_state *state)
942{
943 struct i810_card *card = state->card;
944 unsigned long flags;
945
946 spin_lock_irqsave(&card->lock, flags);
947 __start_adc(state);
948 spin_unlock_irqrestore(&card->lock, flags);
949}
950
951/* stop playback (lock held) */
952static inline void __stop_dac(struct i810_state *state)
953{
954 struct dmabuf *dmabuf = &state->dmabuf;
955 struct i810_card *card = state->card;
956
957 dmabuf->enable &= ~DAC_RUNNING;
958 I810_IOWRITEB(0, card, PO_CR);
959 // wait for the card to acknowledge shutdown
960 while( I810_IOREADB(card, PO_CR) != 0 ) ;
961 // now clear any latent interrupt bits (like the halt bit)
962 if(card->pci_id == PCI_DEVICE_ID_SI_7012)
963 I810_IOWRITEB( I810_IOREADB(card, PO_PICB), card, PO_PICB );
964 else
965 I810_IOWRITEB( I810_IOREADB(card, PO_SR), card, PO_SR );
966 I810_IOWRITEL( I810_IOREADL(card, GLOB_STA) & INT_PO, card, GLOB_STA);
967}
968
969static void stop_dac(struct i810_state *state)
970{
971 struct i810_card *card = state->card;
972 unsigned long flags;
973
974 spin_lock_irqsave(&card->lock, flags);
975 __stop_dac(state);
976 spin_unlock_irqrestore(&card->lock, flags);
977}
978
979static inline void __start_dac(struct i810_state *state)
980{
981 struct dmabuf *dmabuf = &state->dmabuf;
982
983 if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
984 (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
985 dmabuf->enable |= DAC_RUNNING;
986 // Interrupt enable, LVI enable, DMA enable
987 I810_IOWRITEB(0x10 | 0x04 | 0x01, state->card, PO_CR);
988 }
989}
990static void start_dac(struct i810_state *state)
991{
992 struct i810_card *card = state->card;
993 unsigned long flags;
994
995 spin_lock_irqsave(&card->lock, flags);
996 __start_dac(state);
997 spin_unlock_irqrestore(&card->lock, flags);
998}
999
1000#define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
1001#define DMABUF_MINORDER 1
1002
1003/* allocate DMA buffer, playback and recording buffer should be allocated separately */
1004static int alloc_dmabuf(struct i810_state *state)
1005{
1006 struct dmabuf *dmabuf = &state->dmabuf;
1007 void *rawbuf= NULL;
1008 int order, size;
1009 struct page *page, *pend;
1010
1011 /* If we don't have any oss frag params, then use our default ones */
1012 if(dmabuf->ossmaxfrags == 0)
1013 dmabuf->ossmaxfrags = 4;
1014 if(dmabuf->ossfragsize == 0)
1015 dmabuf->ossfragsize = (PAGE_SIZE<<DMABUF_DEFAULTORDER)/dmabuf->ossmaxfrags;
1016 size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
1017
1018 if(dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
1019 return 0;
1020 /* alloc enough to satisfy the oss params */
1021 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
1022 if ( (PAGE_SIZE<<order) > size )
1023 continue;
1024 if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
1025 PAGE_SIZE << order,
1026 &dmabuf->dma_handle)))
1027 break;
1028 }
1029 if (!rawbuf)
1030 return -ENOMEM;
1031
1032
1033#ifdef DEBUG
1034 printk("i810_audio: allocated %ld (order = %d) bytes at %p\n",
1035 PAGE_SIZE << order, order, rawbuf);
1036#endif
1037
1038 dmabuf->ready = dmabuf->mapped = 0;
1039 dmabuf->rawbuf = rawbuf;
1040 dmabuf->buforder = order;
1041
1042 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
1043 pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
1044 for (page = virt_to_page(rawbuf); page <= pend; page++)
1045 SetPageReserved(page);
1046
1047 return 0;
1048}
1049
1050/* free DMA buffer */
1051static void dealloc_dmabuf(struct i810_state *state)
1052{
1053 struct dmabuf *dmabuf = &state->dmabuf;
1054 struct page *page, *pend;
1055
1056 if (dmabuf->rawbuf) {
1057 /* undo marking the pages as reserved */
1058 pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
1059 for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
1060 ClearPageReserved(page);
1061 pci_free_consistent(state->card->pci_dev, PAGE_SIZE << dmabuf->buforder,
1062 dmabuf->rawbuf, dmabuf->dma_handle);
1063 }
1064 dmabuf->rawbuf = NULL;
1065 dmabuf->mapped = dmabuf->ready = 0;
1066}
1067
1068static int prog_dmabuf(struct i810_state *state, unsigned rec)
1069{
1070 struct dmabuf *dmabuf = &state->dmabuf;
1071 struct i810_channel *c;
1072 struct sg_item *sg;
1073 unsigned long flags;
1074 int ret;
1075 unsigned fragint;
1076 int i;
1077
1078 spin_lock_irqsave(&state->card->lock, flags);
1079 if(dmabuf->enable & DAC_RUNNING)
1080 __stop_dac(state);
1081 if(dmabuf->enable & ADC_RUNNING)
1082 __stop_adc(state);
1083 dmabuf->total_bytes = 0;
1084 dmabuf->count = dmabuf->error = 0;
1085 dmabuf->swptr = dmabuf->hwptr = 0;
1086 spin_unlock_irqrestore(&state->card->lock, flags);
1087
1088 /* allocate DMA buffer, let alloc_dmabuf determine if we are already
1089 * allocated well enough or if we should replace the current buffer
1090 * (assuming one is already allocated, if it isn't, then allocate it).
1091 */
1092 if ((ret = alloc_dmabuf(state)))
1093 return ret;
1094
1095 /* FIXME: figure out all this OSS fragment stuff */
1096 /* I did, it now does what it should according to the OSS API. DL */
1097 /* We may not have realloced our dmabuf, but the fragment size to
1098 * fragment number ratio may have changed, so go ahead and reprogram
1099 * things
1100 */
1101 dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
1102 dmabuf->numfrag = SG_LEN;
1103 dmabuf->fragsize = dmabuf->dmasize/dmabuf->numfrag;
1104 dmabuf->fragsamples = dmabuf->fragsize >> 1;
1105 dmabuf->fragshift = ffs(dmabuf->fragsize) - 1;
1106 dmabuf->userfragsize = dmabuf->ossfragsize;
1107 dmabuf->userfrags = dmabuf->dmasize/dmabuf->ossfragsize;
1108
1109 memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
1110
1111 if(dmabuf->ossmaxfrags == 4) {
1112 fragint = 8;
1113 } else if (dmabuf->ossmaxfrags == 8) {
1114 fragint = 4;
1115 } else if (dmabuf->ossmaxfrags == 16) {
1116 fragint = 2;
1117 } else {
1118 fragint = 1;
1119 }
1120 /*
1121 * Now set up the ring
1122 */
1123 if(dmabuf->read_channel)
1124 c = dmabuf->read_channel;
1125 else
1126 c = dmabuf->write_channel;
1127 while(c != NULL) {
1128 sg=&c->sg[0];
1129 /*
1130 * Load up 32 sg entries and take an interrupt at half
1131 * way (we might want more interrupts later..)
1132 */
1133
1134 for(i=0;i<dmabuf->numfrag;i++)
1135 {
1136 sg->busaddr=(u32)dmabuf->dma_handle+dmabuf->fragsize*i;
1137 // the card will always be doing 16bit stereo
1138 sg->control=dmabuf->fragsamples;
1139 if(state->card->pci_id == PCI_DEVICE_ID_SI_7012)
1140 sg->control <<= 1;
1141 sg->control|=CON_BUFPAD;
1142 // set us up to get IOC interrupts as often as needed to
1143 // satisfy numfrag requirements, no more
1144 if( ((i+1) % fragint) == 0) {
1145 sg->control|=CON_IOC;
1146 }
1147 sg++;
1148 }
1149 spin_lock_irqsave(&state->card->lock, flags);
1150 I810_IOWRITEB(2, state->card, c->port+OFF_CR); /* reset DMA machine */
1151 while( I810_IOREADB(state->card, c->port+OFF_CR) & 0x02 ) ;
1152 I810_IOWRITEL((u32)state->card->chandma +
1153 c->num*sizeof(struct i810_channel),
1154 state->card, c->port+OFF_BDBAR);
1155 CIV_TO_LVI(state->card, c->port, 0);
1156
1157 spin_unlock_irqrestore(&state->card->lock, flags);
1158
1159 if(c != dmabuf->write_channel)
1160 c = dmabuf->write_channel;
1161 else
1162 c = NULL;
1163 }
1164
1165 /* set the ready flag for the dma buffer */
1166 dmabuf->ready = 1;
1167
1168#ifdef DEBUG
1169 printk("i810_audio: prog_dmabuf, sample rate = %d, format = %d,\n\tnumfrag = %d, "
1170 "fragsize = %d dmasize = %d\n",
1171 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1172 dmabuf->fragsize, dmabuf->dmasize);
1173#endif
1174
1175 return 0;
1176}
1177
1178static void __i810_update_lvi(struct i810_state *state, int rec)
1179{
1180 struct dmabuf *dmabuf = &state->dmabuf;
1181 int x, port;
1182 int trigger;
1183 int count, fragsize;
1184 void (*start)(struct i810_state *);
1185
1186 count = dmabuf->count;
1187 if (rec) {
1188 port = dmabuf->read_channel->port;
1189 trigger = PCM_ENABLE_INPUT;
1190 start = __start_adc;
1191 count = dmabuf->dmasize - count;
1192 } else {
1193 port = dmabuf->write_channel->port;
1194 trigger = PCM_ENABLE_OUTPUT;
1195 start = __start_dac;
1196 }
1197
1198 /* Do not process partial fragments. */
1199 fragsize = dmabuf->fragsize;
1200 if (count < fragsize)
1201 return;
1202
1203 /* if we are currently stopped, then our CIV is actually set to our
1204 * *last* sg segment and we are ready to wrap to the next. However,
1205 * if we set our LVI to the last sg segment, then it won't wrap to
1206 * the next sg segment, it won't even get a start. So, instead, when
1207 * we are stopped, we set both the LVI value and also we increment
1208 * the CIV value to the next sg segment to be played so that when
1209 * we call start, things will operate properly. Since the CIV can't
1210 * be written to directly for this purpose, we set the LVI to CIV + 1
1211 * temporarily. Once the engine has started we set the LVI to its
1212 * final value.
1213 */
1214 if (!dmabuf->enable && dmabuf->ready) {
1215 if (!(dmabuf->trigger & trigger))
1216 return;
1217
1218 CIV_TO_LVI(state->card, port, 1);
1219
1220 start(state);
1221 while (!(I810_IOREADB(state->card, port + OFF_CR) & ((1<<4) | (1<<2))))
1222 ;
1223 }
1224
1225 /* MASKP2(swptr, fragsize) - 1 is the tail of our transfer */
1226 x = MODULOP2(MASKP2(dmabuf->swptr, fragsize) - 1, dmabuf->dmasize);
1227 x >>= dmabuf->fragshift;
1228 I810_IOWRITEB(x, state->card, port + OFF_LVI);
1229}
1230
1231static void i810_update_lvi(struct i810_state *state, int rec)
1232{
1233 struct dmabuf *dmabuf = &state->dmabuf;
1234 unsigned long flags;
1235
1236 if(!dmabuf->ready)
1237 return;
1238 spin_lock_irqsave(&state->card->lock, flags);
1239 __i810_update_lvi(state, rec);
1240 spin_unlock_irqrestore(&state->card->lock, flags);
1241}
1242
1243/* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1244static void i810_update_ptr(struct i810_state *state)
1245{
1246 struct dmabuf *dmabuf = &state->dmabuf;
1247 unsigned hwptr;
1248 unsigned fragmask, dmamask;
1249 int diff;
1250
1251 fragmask = MASKP2(~0, dmabuf->fragsize);
1252 dmamask = MODULOP2(~0, dmabuf->dmasize);
1253
1254 /* error handling and process wake up for ADC */
1255 if (dmabuf->enable == ADC_RUNNING) {
1256 /* update hardware pointer */
1257 hwptr = i810_get_dma_addr(state, 1) & fragmask;
1258 diff = (hwptr - dmabuf->hwptr) & dmamask;
1259#if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
1260 printk("ADC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
1261#endif
1262 dmabuf->hwptr = hwptr;
1263 dmabuf->total_bytes += diff;
1264 dmabuf->count += diff;
1265 if (dmabuf->count > dmabuf->dmasize) {
1266 /* buffer underrun or buffer overrun */
1267 /* this is normal for the end of a read */
1268 /* only give an error if we went past the */
1269 /* last valid sg entry */
1270 if (GET_CIV(state->card, PI_BASE) !=
1271 GET_LVI(state->card, PI_BASE)) {
1272 printk(KERN_WARNING "i810_audio: DMA overrun on read\n");
1273 dmabuf->error++;
1274 }
1275 }
1276 if (diff)
1277 wake_up(&dmabuf->wait);
1278 }
1279 /* error handling and process wake up for DAC */
1280 if (dmabuf->enable == DAC_RUNNING) {
1281 /* update hardware pointer */
1282 hwptr = i810_get_dma_addr(state, 0) & fragmask;
1283 diff = (hwptr - dmabuf->hwptr) & dmamask;
1284#if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
1285 printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
1286#endif
1287 dmabuf->hwptr = hwptr;
1288 dmabuf->total_bytes += diff;
1289 dmabuf->count -= diff;
1290 if (dmabuf->count < 0) {
1291 /* buffer underrun or buffer overrun */
1292 /* this is normal for the end of a write */
1293 /* only give an error if we went past the */
1294 /* last valid sg entry */
1295 if (GET_CIV(state->card, PO_BASE) !=
1296 GET_LVI(state->card, PO_BASE)) {
1297 printk(KERN_WARNING "i810_audio: DMA overrun on write\n");
1298 printk("i810_audio: CIV %d, LVI %d, hwptr %x, "
1299 "count %d\n",
1300 GET_CIV(state->card, PO_BASE),
1301 GET_LVI(state->card, PO_BASE),
1302 dmabuf->hwptr, dmabuf->count);
1303 dmabuf->error++;
1304 }
1305 }
1306 if (diff)
1307 wake_up(&dmabuf->wait);
1308 }
1309}
1310
1311static inline int i810_get_free_write_space(struct i810_state *state)
1312{
1313 struct dmabuf *dmabuf = &state->dmabuf;
1314 int free;
1315
1316 i810_update_ptr(state);
1317 // catch underruns during playback
1318 if (dmabuf->count < 0) {
1319 dmabuf->count = 0;
1320 dmabuf->swptr = dmabuf->hwptr;
1321 }
1322 free = dmabuf->dmasize - dmabuf->count;
1323 if(free < 0)
1324 return(0);
1325 return(free);
1326}
1327
1328static inline int i810_get_available_read_data(struct i810_state *state)
1329{
1330 struct dmabuf *dmabuf = &state->dmabuf;
1331 int avail;
1332
1333 i810_update_ptr(state);
1334 // catch overruns during record
1335 if (dmabuf->count > dmabuf->dmasize) {
1336 dmabuf->count = dmabuf->dmasize;
1337 dmabuf->swptr = dmabuf->hwptr;
1338 }
1339 avail = dmabuf->count;
1340 if(avail < 0)
1341 return(0);
1342 return(avail);
1343}
1344
1345static inline void fill_partial_frag(struct dmabuf *dmabuf)
1346{
1347 unsigned fragsize;
1348 unsigned swptr, len;
1349
1350 fragsize = dmabuf->fragsize;
1351 swptr = dmabuf->swptr;
1352 len = fragsize - MODULOP2(dmabuf->swptr, fragsize);
1353 if (len == fragsize)
1354 return;
1355
1356 memset(dmabuf->rawbuf + swptr, '\0', len);
1357 dmabuf->swptr = MODULOP2(swptr + len, dmabuf->dmasize);
1358 dmabuf->count += len;
1359}
1360
1361static int drain_dac(struct i810_state *state, int signals_allowed)
1362{
1363 DECLARE_WAITQUEUE(wait, current);
1364 struct dmabuf *dmabuf = &state->dmabuf;
1365 unsigned long flags;
1366 unsigned long tmo;
1367 int count;
1368
1369 if (!dmabuf->ready)
1370 return 0;
1371 if(dmabuf->mapped) {
1372 stop_dac(state);
1373 return 0;
1374 }
1375
1376 spin_lock_irqsave(&state->card->lock, flags);
1377
1378 fill_partial_frag(dmabuf);
1379
1380 /*
1381 * This will make sure that our LVI is correct, that our
1382 * pointer is updated, and that the DAC is running. We
1383 * have to force the setting of dmabuf->trigger to avoid
1384 * any possible deadlocks.
1385 */
1386 dmabuf->trigger = PCM_ENABLE_OUTPUT;
1387 __i810_update_lvi(state, 0);
1388
1389 spin_unlock_irqrestore(&state->card->lock, flags);
1390
1391 add_wait_queue(&dmabuf->wait, &wait);
1392 for (;;) {
1393
1394 spin_lock_irqsave(&state->card->lock, flags);
1395 i810_update_ptr(state);
1396 count = dmabuf->count;
1397
1398 /* It seems that we have to set the current state to
1399 * TASK_INTERRUPTIBLE every time to make the process
1400 * really go to sleep. This also has to be *after* the
1401 * update_ptr() call because update_ptr is likely to
1402 * do a wake_up() which will unset this before we ever
1403 * try to sleep, resuling in a tight loop in this code
1404 * instead of actually sleeping and waiting for an
1405 * interrupt to wake us up!
1406 */
1407 __set_current_state(signals_allowed ?
1408 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
1409 spin_unlock_irqrestore(&state->card->lock, flags);
1410
1411 if (count <= 0)
1412 break;
1413
1414 if (signal_pending(current) && signals_allowed) {
1415 break;
1416 }
1417
1418 /*
1419 * set the timeout to significantly longer than it *should*
1420 * take for the DAC to drain the DMA buffer
1421 */
1422 tmo = (count * HZ) / (dmabuf->rate);
1423 if (!schedule_timeout(tmo >= 2 ? tmo : 2)){
1424 printk(KERN_ERR "i810_audio: drain_dac, dma timeout?\n");
1425 count = 0;
1426 break;
1427 }
1428 }
1429 set_current_state(TASK_RUNNING);
1430 remove_wait_queue(&dmabuf->wait, &wait);
1431 if(count > 0 && signal_pending(current) && signals_allowed)
1432 return -ERESTARTSYS;
1433 stop_dac(state);
1434 return 0;
1435}
1436
1437static void i810_channel_interrupt(struct i810_card *card)
1438{
1439 int i, count;
1440
1441#ifdef DEBUG_INTERRUPTS
1442 printk("CHANNEL ");
1443#endif
1444 for(i=0;i<NR_HW_CH;i++)
1445 {
1446 struct i810_state *state = card->states[i];
1447 struct i810_channel *c;
1448 struct dmabuf *dmabuf;
1449 unsigned long port;
1450 u16 status;
1451
1452 if(!state)
1453 continue;
1454 if(!state->dmabuf.ready)
1455 continue;
1456 dmabuf = &state->dmabuf;
1457 if(dmabuf->enable & DAC_RUNNING) {
1458 c=dmabuf->write_channel;
1459 } else if(dmabuf->enable & ADC_RUNNING) {
1460 c=dmabuf->read_channel;
1461 } else /* This can occur going from R/W to close */
1462 continue;
1463
1464 port = c->port;
1465
1466 if(card->pci_id == PCI_DEVICE_ID_SI_7012)
1467 status = I810_IOREADW(card, port + OFF_PICB);
1468 else
1469 status = I810_IOREADW(card, port + OFF_SR);
1470
1471#ifdef DEBUG_INTERRUPTS
1472 printk("NUM %d PORT %X IRQ ( ST%d ", c->num, c->port, status);
1473#endif
1474 if(status & DMA_INT_COMPLETE)
1475 {
1476 /* only wake_up() waiters if this interrupt signals
1477 * us being beyond a userfragsize of data open or
1478 * available, and i810_update_ptr() does that for
1479 * us
1480 */
1481 i810_update_ptr(state);
1482#ifdef DEBUG_INTERRUPTS
1483 printk("COMP %d ", dmabuf->hwptr /
1484 dmabuf->fragsize);
1485#endif
1486 }
1487 if(status & (DMA_INT_LVI | DMA_INT_DCH))
1488 {
1489 /* wake_up() unconditionally on LVI and DCH */
1490 i810_update_ptr(state);
1491 wake_up(&dmabuf->wait);
1492#ifdef DEBUG_INTERRUPTS
1493 if(status & DMA_INT_LVI)
1494 printk("LVI ");
1495 if(status & DMA_INT_DCH)
1496 printk("DCH -");
1497#endif
1498 count = dmabuf->count;
1499 if(dmabuf->enable & ADC_RUNNING)
1500 count = dmabuf->dmasize - count;
1501 if (count >= (int)dmabuf->fragsize) {
1502 I810_IOWRITEB(I810_IOREADB(card, port+OFF_CR) | 1, card, port+OFF_CR);
1503#ifdef DEBUG_INTERRUPTS
1504 printk(" CONTINUE ");
1505#endif
1506 } else {
1507 if (dmabuf->enable & DAC_RUNNING)
1508 __stop_dac(state);
1509 if (dmabuf->enable & ADC_RUNNING)
1510 __stop_adc(state);
1511 dmabuf->enable = 0;
1512#ifdef DEBUG_INTERRUPTS
1513 printk(" STOP ");
1514#endif
1515 }
1516 }
1517 if(card->pci_id == PCI_DEVICE_ID_SI_7012)
1518 I810_IOWRITEW(status & DMA_INT_MASK, card, port + OFF_PICB);
1519 else
1520 I810_IOWRITEW(status & DMA_INT_MASK, card, port + OFF_SR);
1521 }
1522#ifdef DEBUG_INTERRUPTS
1523 printk(")\n");
1524#endif
1525}
1526
1527static irqreturn_t i810_interrupt(int irq, void *dev_id)
1528{
1529 struct i810_card *card = dev_id;
1530 u32 status;
1531
1532 spin_lock(&card->lock);
1533
1534 status = I810_IOREADL(card, GLOB_STA);
1535
1536 if(!(status & INT_MASK))
1537 {
1538 spin_unlock(&card->lock);
1539 return IRQ_NONE; /* not for us */
1540 }
1541
1542 if(status & (INT_PO|INT_PI|INT_MC))
1543 i810_channel_interrupt(card);
1544
1545 /* clear 'em */
1546 I810_IOWRITEL(status & INT_MASK, card, GLOB_STA);
1547 spin_unlock(&card->lock);
1548 return IRQ_HANDLED;
1549}
1550
1551/* in this loop, dmabuf.count signifies the amount of data that is
1552 waiting to be copied to the user's buffer. It is filled by the dma
1553 machine and drained by this loop. */
1554
1555static ssize_t i810_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1556{
1557 struct i810_state *state = (struct i810_state *)file->private_data;
1558 struct i810_card *card=state ? state->card : NULL;
1559 struct dmabuf *dmabuf = &state->dmabuf;
1560 ssize_t ret;
1561 unsigned long flags;
1562 unsigned int swptr;
1563 int cnt;
1564 int pending;
1565 DECLARE_WAITQUEUE(waita, current);
1566
1567#ifdef DEBUG2
1568 printk("i810_audio: i810_read called, count = %d\n", count);
1569#endif
1570
1571 if (dmabuf->mapped)
1572 return -ENXIO;
1573 if (dmabuf->enable & DAC_RUNNING)
1574 return -ENODEV;
1575 if (!dmabuf->read_channel) {
1576 dmabuf->ready = 0;
1577 dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
1578 if (!dmabuf->read_channel) {
1579 return -EBUSY;
1580 }
1581 }
1582 if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1583 return ret;
1584 if (!access_ok(VERIFY_WRITE, buffer, count))
1585 return -EFAULT;
1586 ret = 0;
1587
1588 pending = 0;
1589
1590 add_wait_queue(&dmabuf->wait, &waita);
1591 while (count > 0) {
1592 set_current_state(TASK_INTERRUPTIBLE);
1593 spin_lock_irqsave(&card->lock, flags);
1594 if (PM_SUSPENDED(card)) {
1595 spin_unlock_irqrestore(&card->lock, flags);
1596 schedule();
1597 if (signal_pending(current)) {
1598 if (!ret) ret = -EAGAIN;
1599 break;
1600 }
1601 continue;
1602 }
1603 cnt = i810_get_available_read_data(state);
1604 swptr = dmabuf->swptr;
1605 // this is to make the copy_to_user simpler below
1606 if(cnt > (dmabuf->dmasize - swptr))
1607 cnt = dmabuf->dmasize - swptr;
1608 spin_unlock_irqrestore(&card->lock, flags);
1609
1610 if (cnt > count)
1611 cnt = count;
1612 if (cnt <= 0) {
1613 unsigned long tmo;
1614 /*
1615 * Don't let us deadlock. The ADC won't start if
1616 * dmabuf->trigger isn't set. A call to SETTRIGGER
1617 * could have turned it off after we set it to on
1618 * previously.
1619 */
1620 dmabuf->trigger = PCM_ENABLE_INPUT;
1621 /*
1622 * This does three things. Updates LVI to be correct,
1623 * makes sure the ADC is running, and updates the
1624 * hwptr.
1625 */
1626 i810_update_lvi(state,1);
1627 if (file->f_flags & O_NONBLOCK) {
1628 if (!ret) ret = -EAGAIN;
1629 goto done;
1630 }
1631 /* Set the timeout to how long it would take to fill
1632 * two of our buffers. If we haven't been woke up
1633 * by then, then we know something is wrong.
1634 */
1635 tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1636 /* There are two situations when sleep_on_timeout returns, one is when
1637 the interrupt is serviced correctly and the process is waked up by
1638 ISR ON TIME. Another is when timeout is expired, which means that
1639 either interrupt is NOT serviced correctly (pending interrupt) or it
1640 is TOO LATE for the process to be scheduled to run (scheduler latency)
1641 which results in a (potential) buffer overrun. And worse, there is
1642 NOTHING we can do to prevent it. */
1643 if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1644#ifdef DEBUG
1645 printk(KERN_ERR "i810_audio: recording schedule timeout, "
1646 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1647 dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1648 dmabuf->hwptr, dmabuf->swptr);
1649#endif
1650 /* a buffer overrun, we delay the recovery until next time the
1651 while loop begin and we REALLY have space to record */
1652 }
1653 if (signal_pending(current)) {
1654 ret = ret ? ret : -ERESTARTSYS;
1655 goto done;
1656 }
1657 continue;
1658 }
1659
1660 if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1661 if (!ret) ret = -EFAULT;
1662 goto done;
1663 }
1664
1665 swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
1666
1667 spin_lock_irqsave(&card->lock, flags);
1668
1669 if (PM_SUSPENDED(card)) {
1670 spin_unlock_irqrestore(&card->lock, flags);
1671 continue;
1672 }
1673 dmabuf->swptr = swptr;
1674 pending = dmabuf->count -= cnt;
1675 spin_unlock_irqrestore(&card->lock, flags);
1676
1677 count -= cnt;
1678 buffer += cnt;
1679 ret += cnt;
1680 }
1681 done:
1682 pending = dmabuf->dmasize - pending;
1683 if (dmabuf->enable || pending >= dmabuf->userfragsize)
1684 i810_update_lvi(state, 1);
1685 set_current_state(TASK_RUNNING);
1686 remove_wait_queue(&dmabuf->wait, &waita);
1687
1688 return ret;
1689}
1690
1691/* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1692 the soundcard. it is drained by the dma machine and filled by this loop. */
1693static ssize_t i810_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1694{
1695 struct i810_state *state = (struct i810_state *)file->private_data;
1696 struct i810_card *card=state ? state->card : NULL;
1697 struct dmabuf *dmabuf = &state->dmabuf;
1698 ssize_t ret;
1699 unsigned long flags;
1700 unsigned int swptr = 0;
1701 int pending;
1702 int cnt;
1703 DECLARE_WAITQUEUE(waita, current);
1704
1705#ifdef DEBUG2
1706 printk("i810_audio: i810_write called, count = %d\n", count);
1707#endif
1708
1709 if (dmabuf->mapped)
1710 return -ENXIO;
1711 if (dmabuf->enable & ADC_RUNNING)
1712 return -ENODEV;
1713 if (!dmabuf->write_channel) {
1714 dmabuf->ready = 0;
1715 dmabuf->write_channel = card->alloc_pcm_channel(card);
1716 if(!dmabuf->write_channel)
1717 return -EBUSY;
1718 }
1719 if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1720 return ret;
1721 if (!access_ok(VERIFY_READ, buffer, count))
1722 return -EFAULT;
1723 ret = 0;
1724
1725 pending = 0;
1726
1727 add_wait_queue(&dmabuf->wait, &waita);
1728 while (count > 0) {
1729 set_current_state(TASK_INTERRUPTIBLE);
1730 spin_lock_irqsave(&state->card->lock, flags);
1731 if (PM_SUSPENDED(card)) {
1732 spin_unlock_irqrestore(&card->lock, flags);
1733 schedule();
1734 if (signal_pending(current)) {
1735 if (!ret) ret = -EAGAIN;
1736 break;
1737 }
1738 continue;
1739 }
1740
1741 cnt = i810_get_free_write_space(state);
1742 swptr = dmabuf->swptr;
1743 /* Bound the maximum size to how much we can copy to the
1744 * dma buffer before we hit the end. If we have more to
1745 * copy then it will get done in a second pass of this
1746 * loop starting from the beginning of the buffer.
1747 */
1748 if(cnt > (dmabuf->dmasize - swptr))
1749 cnt = dmabuf->dmasize - swptr;
1750 spin_unlock_irqrestore(&state->card->lock, flags);
1751
1752#ifdef DEBUG2
1753 printk(KERN_INFO "i810_audio: i810_write: %d bytes available space\n", cnt);
1754#endif
1755 if (cnt > count)
1756 cnt = count;
1757 if (cnt <= 0) {
1758 unsigned long tmo;
1759 // There is data waiting to be played
1760 /*
1761 * Force the trigger setting since we would
1762 * deadlock with it set any other way
1763 */
1764 dmabuf->trigger = PCM_ENABLE_OUTPUT;
1765 i810_update_lvi(state,0);
1766 if (file->f_flags & O_NONBLOCK) {
1767 if (!ret) ret = -EAGAIN;
1768 goto ret;
1769 }
1770 /* Not strictly correct but works */
1771 tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1772 /* There are two situations when sleep_on_timeout returns, one is when
1773 the interrupt is serviced correctly and the process is waked up by
1774 ISR ON TIME. Another is when timeout is expired, which means that
1775 either interrupt is NOT serviced correctly (pending interrupt) or it
1776 is TOO LATE for the process to be scheduled to run (scheduler latency)
1777 which results in a (potential) buffer underrun. And worse, there is
1778 NOTHING we can do to prevent it. */
1779 if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1780#ifdef DEBUG
1781 printk(KERN_ERR "i810_audio: playback schedule timeout, "
1782 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1783 dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1784 dmabuf->hwptr, dmabuf->swptr);
1785#endif
1786 /* a buffer underrun, we delay the recovery until next time the
1787 while loop begin and we REALLY have data to play */
1788 //return ret;
1789 }
1790 if (signal_pending(current)) {
1791 if (!ret) ret = -ERESTARTSYS;
1792 goto ret;
1793 }
1794 continue;
1795 }
1796 if (copy_from_user(dmabuf->rawbuf+swptr,buffer,cnt)) {
1797 if (!ret) ret = -EFAULT;
1798 goto ret;
1799 }
1800
1801 swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
1802
1803 spin_lock_irqsave(&state->card->lock, flags);
1804 if (PM_SUSPENDED(card)) {
1805 spin_unlock_irqrestore(&card->lock, flags);
1806 continue;
1807 }
1808
1809 dmabuf->swptr = swptr;
1810 pending = dmabuf->count += cnt;
1811
1812 count -= cnt;
1813 buffer += cnt;
1814 ret += cnt;
1815 spin_unlock_irqrestore(&state->card->lock, flags);
1816 }
1817ret:
1818 if (dmabuf->enable || pending >= dmabuf->userfragsize)
1819 i810_update_lvi(state, 0);
1820 set_current_state(TASK_RUNNING);
1821 remove_wait_queue(&dmabuf->wait, &waita);
1822
1823 return ret;
1824}
1825
1826/* No kernel lock - we have our own spinlock */
1827static unsigned int i810_poll(struct file *file, struct poll_table_struct *wait)
1828{
1829 struct i810_state *state = (struct i810_state *)file->private_data;
1830 struct dmabuf *dmabuf = &state->dmabuf;
1831 unsigned long flags;
1832 unsigned int mask = 0;
1833
1834 if(!dmabuf->ready)
1835 return 0;
1836 poll_wait(file, &dmabuf->wait, wait);
1837 spin_lock_irqsave(&state->card->lock, flags);
1838 if (dmabuf->enable & ADC_RUNNING ||
1839 dmabuf->trigger & PCM_ENABLE_INPUT) {
1840 if (i810_get_available_read_data(state) >=
1841 (signed)dmabuf->userfragsize)
1842 mask |= POLLIN | POLLRDNORM;
1843 }
1844 if (dmabuf->enable & DAC_RUNNING ||
1845 dmabuf->trigger & PCM_ENABLE_OUTPUT) {
1846 if (i810_get_free_write_space(state) >=
1847 (signed)dmabuf->userfragsize)
1848 mask |= POLLOUT | POLLWRNORM;
1849 }
1850 spin_unlock_irqrestore(&state->card->lock, flags);
1851 return mask;
1852}
1853
1854static int i810_mmap(struct file *file, struct vm_area_struct *vma)
1855{
1856 struct i810_state *state = (struct i810_state *)file->private_data;
1857 struct dmabuf *dmabuf = &state->dmabuf;
1858 int ret = -EINVAL;
1859 unsigned long size;
1860
1861 lock_kernel();
1862 if (vma->vm_flags & VM_WRITE) {
1863 if (!dmabuf->write_channel &&
1864 (dmabuf->write_channel =
1865 state->card->alloc_pcm_channel(state->card)) == NULL) {
1866 ret = -EBUSY;
1867 goto out;
1868 }
1869 }
1870 if (vma->vm_flags & VM_READ) {
1871 if (!dmabuf->read_channel &&
1872 (dmabuf->read_channel =
1873 state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
1874 ret = -EBUSY;
1875 goto out;
1876 }
1877 }
1878 if ((ret = prog_dmabuf(state, 0)) != 0)
1879 goto out;
1880
1881 ret = -EINVAL;
1882 if (vma->vm_pgoff != 0)
1883 goto out;
1884 size = vma->vm_end - vma->vm_start;
1885 if (size > (PAGE_SIZE << dmabuf->buforder))
1886 goto out;
1887 ret = -EAGAIN;
1888 if (remap_pfn_range(vma, vma->vm_start,
1889 virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
1890 size, vma->vm_page_prot))
1891 goto out;
1892 dmabuf->mapped = 1;
1893 dmabuf->trigger = 0;
1894 ret = 0;
1895#ifdef DEBUG_MMAP
1896 printk("i810_audio: mmap'ed %ld bytes of data space\n", size);
1897#endif
1898out:
1899 unlock_kernel();
1900 return ret;
1901}
1902
1903static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1904{
1905 struct i810_state *state = (struct i810_state *)file->private_data;
1906 struct i810_channel *c = NULL;
1907 struct dmabuf *dmabuf = &state->dmabuf;
1908 unsigned long flags;
1909 audio_buf_info abinfo;
1910 count_info cinfo;
1911 unsigned int i_glob_cnt;
1912 int val = 0, ret;
1913 struct ac97_codec *codec = state->card->ac97_codec[0];
1914 void __user *argp = (void __user *)arg;
1915 int __user *p = argp;
1916
1917#ifdef DEBUG
1918 printk("i810_audio: i810_ioctl, arg=0x%x, cmd=", arg ? *p : 0);
1919#endif
1920
1921 switch (cmd)
1922 {
1923 case OSS_GETVERSION:
1924#ifdef DEBUG
1925 printk("OSS_GETVERSION\n");
1926#endif
1927 return put_user(SOUND_VERSION, p);
1928
1929 case SNDCTL_DSP_RESET:
1930#ifdef DEBUG
1931 printk("SNDCTL_DSP_RESET\n");
1932#endif
1933 spin_lock_irqsave(&state->card->lock, flags);
1934 if (dmabuf->enable == DAC_RUNNING) {
1935 c = dmabuf->write_channel;
1936 __stop_dac(state);
1937 }
1938 if (dmabuf->enable == ADC_RUNNING) {
1939 c = dmabuf->read_channel;
1940 __stop_adc(state);
1941 }
1942 if (c != NULL) {
1943 I810_IOWRITEB(2, state->card, c->port+OFF_CR); /* reset DMA machine */
1944 while ( I810_IOREADB(state->card, c->port+OFF_CR) & 2 )
1945 cpu_relax();
1946 I810_IOWRITEL((u32)state->card->chandma +
1947 c->num*sizeof(struct i810_channel),
1948 state->card, c->port+OFF_BDBAR);
1949 CIV_TO_LVI(state->card, c->port, 0);
1950 }
1951
1952 spin_unlock_irqrestore(&state->card->lock, flags);
1953 synchronize_irq(state->card->pci_dev->irq);
1954 dmabuf->ready = 0;
1955 dmabuf->swptr = dmabuf->hwptr = 0;
1956 dmabuf->count = dmabuf->total_bytes = 0;
1957 return 0;
1958
1959 case SNDCTL_DSP_SYNC:
1960#ifdef DEBUG
1961 printk("SNDCTL_DSP_SYNC\n");
1962#endif
1963 if (dmabuf->enable != DAC_RUNNING || file->f_flags & O_NONBLOCK)
1964 return 0;
1965 if((val = drain_dac(state, 1)))
1966 return val;
1967 dmabuf->total_bytes = 0;
1968 return 0;
1969
1970 case SNDCTL_DSP_SPEED: /* set smaple rate */
1971#ifdef DEBUG
1972 printk("SNDCTL_DSP_SPEED\n");
1973#endif
1974 if (get_user(val, p))
1975 return -EFAULT;
1976 if (val >= 0) {
1977 if (file->f_mode & FMODE_WRITE) {
1978 if ( (state->card->ac97_status & SPDIF_ON) ) { /* S/PDIF Enabled */
1979 /* AD1886 only supports 48000, need to check that */
1980 if ( i810_valid_spdif_rate ( codec, val ) ) {
1981 /* Set DAC rate */
1982 i810_set_spdif_output ( state, -1, 0 );
1983 stop_dac(state);
1984 dmabuf->ready = 0;
1985 spin_lock_irqsave(&state->card->lock, flags);
1986 i810_set_dac_rate(state, val);
1987 spin_unlock_irqrestore(&state->card->lock, flags);
1988 /* Set S/PDIF transmitter rate. */
1989 i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, val );
1990 if ( ! (state->card->ac97_status & SPDIF_ON) ) {
1991 val = dmabuf->rate;
1992 }
1993 } else { /* Not a valid rate for S/PDIF, ignore it */
1994 val = dmabuf->rate;
1995 }
1996 } else {
1997 stop_dac(state);
1998 dmabuf->ready = 0;
1999 spin_lock_irqsave(&state->card->lock, flags);
2000 i810_set_dac_rate(state, val);
2001 spin_unlock_irqrestore(&state->card->lock, flags);
2002 }
2003 }
2004 if (file->f_mode & FMODE_READ) {
2005 stop_adc(state);
2006 dmabuf->ready = 0;
2007 spin_lock_irqsave(&state->card->lock, flags);
2008 i810_set_adc_rate(state, val);
2009 spin_unlock_irqrestore(&state->card->lock, flags);
2010 }
2011 }
2012 return put_user(dmabuf->rate, p);
2013
2014 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2015#ifdef DEBUG
2016 printk("SNDCTL_DSP_STEREO\n");
2017#endif
2018 if (dmabuf->enable & DAC_RUNNING) {
2019 stop_dac(state);
2020 }
2021 if (dmabuf->enable & ADC_RUNNING) {
2022 stop_adc(state);
2023 }
2024 return put_user(1, p);
2025
2026 case SNDCTL_DSP_GETBLKSIZE:
2027 if (file->f_mode & FMODE_WRITE) {
2028 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
2029 return val;
2030 }
2031 if (file->f_mode & FMODE_READ) {
2032 if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
2033 return val;
2034 }
2035#ifdef DEBUG
2036 printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
2037#endif
2038 return put_user(dmabuf->userfragsize, p);
2039
2040 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
2041#ifdef DEBUG
2042 printk("SNDCTL_DSP_GETFMTS\n");
2043#endif
2044 return put_user(AFMT_S16_LE, p);
2045
2046 case SNDCTL_DSP_SETFMT: /* Select sample format */
2047#ifdef DEBUG
2048 printk("SNDCTL_DSP_SETFMT\n");
2049#endif
2050 return put_user(AFMT_S16_LE, p);
2051
2052 case SNDCTL_DSP_CHANNELS:
2053#ifdef DEBUG
2054 printk("SNDCTL_DSP_CHANNELS\n");
2055#endif
2056 if (get_user(val, p))
2057 return -EFAULT;
2058
2059 if (val > 0) {
2060 if (dmabuf->enable & DAC_RUNNING) {
2061 stop_dac(state);
2062 }
2063 if (dmabuf->enable & ADC_RUNNING) {
2064 stop_adc(state);
2065 }
2066 } else {
2067 return put_user(state->card->channels, p);
2068 }
2069
2070 /* ICH and ICH0 only support 2 channels */
2071 if ( state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AA_5
2072 || state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AB_5)
2073 return put_user(2, p);
2074
2075 /* Multi-channel support was added with ICH2. Bits in */
2076 /* Global Status and Global Control register are now */
2077 /* used to indicate this. */
2078
2079 i_glob_cnt = I810_IOREADL(state->card, GLOB_CNT);
2080
2081 /* Current # of channels enabled */
2082 if ( i_glob_cnt & 0x0100000 )
2083 ret = 4;
2084 else if ( i_glob_cnt & 0x0200000 )
2085 ret = 6;
2086 else
2087 ret = 2;
2088
2089 switch ( val ) {
2090 case 2: /* 2 channels is always supported */
2091 I810_IOWRITEL(i_glob_cnt & 0xffcfffff,
2092 state->card, GLOB_CNT);
2093 /* Do we need to change mixer settings???? */
2094 break;
2095 case 4: /* Supported on some chipsets, better check first */
2096 if ( state->card->channels >= 4 ) {
2097 I810_IOWRITEL((i_glob_cnt & 0xffcfffff) | 0x100000,
2098 state->card, GLOB_CNT);
2099 /* Do we need to change mixer settings??? */
2100 } else {
2101 val = ret;
2102 }
2103 break;
2104 case 6: /* Supported on some chipsets, better check first */
2105 if ( state->card->channels >= 6 ) {
2106 I810_IOWRITEL((i_glob_cnt & 0xffcfffff) | 0x200000,
2107 state->card, GLOB_CNT);
2108 /* Do we need to change mixer settings??? */
2109 } else {
2110 val = ret;
2111 }
2112 break;
2113 default: /* nothing else is ever supported by the chipset */
2114 val = ret;
2115 break;
2116 }
2117
2118 return put_user(val, p);
2119
2120 case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
2121 /* we update the swptr to the end of the last sg segment then return */
2122#ifdef DEBUG
2123 printk("SNDCTL_DSP_POST\n");
2124#endif
2125 if(!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
2126 return 0;
2127 if((dmabuf->swptr % dmabuf->fragsize) != 0) {
2128 val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
2129 dmabuf->swptr += val;
2130 dmabuf->count += val;
2131 }
2132 return 0;
2133
2134 case SNDCTL_DSP_SUBDIVIDE:
2135 if (dmabuf->subdivision)
2136 return -EINVAL;
2137 if (get_user(val, p))
2138 return -EFAULT;
2139 if (val != 1 && val != 2 && val != 4)
2140 return -EINVAL;
2141#ifdef DEBUG
2142 printk("SNDCTL_DSP_SUBDIVIDE %d\n", val);
2143#endif
2144 dmabuf->subdivision = val;
2145 dmabuf->ready = 0;
2146 return 0;
2147
2148 case SNDCTL_DSP_SETFRAGMENT:
2149 if (get_user(val, p))
2150 return -EFAULT;
2151
2152 dmabuf->ossfragsize = 1<<(val & 0xffff);
2153 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2154 if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
2155 return -EINVAL;
2156 /*
2157 * Bound the frag size into our allowed range of 256 - 4096
2158 */
2159 if (dmabuf->ossfragsize < 256)
2160 dmabuf->ossfragsize = 256;
2161 else if (dmabuf->ossfragsize > 4096)
2162 dmabuf->ossfragsize = 4096;
2163 /*
2164 * The numfrags could be something reasonable, or it could
2165 * be 0xffff meaning "Give me as much as possible". So,
2166 * we check the numfrags * fragsize doesn't exceed our
2167 * 64k buffer limit, nor is it less than our 8k minimum.
2168 * If it fails either one of these checks, then adjust the
2169 * number of fragments, not the size of them. It's OK if
2170 * our number of fragments doesn't equal 32 or anything
2171 * like our hardware based number now since we are using
2172 * a different frag count for the hardware. Before we get
2173 * into this though, bound the maxfrags to avoid overflow
2174 * issues. A reasonable bound would be 64k / 256 since our
2175 * maximum buffer size is 64k and our minimum frag size is
2176 * 256. On the other end, our minimum buffer size is 8k and
2177 * our maximum frag size is 4k, so the lower bound should
2178 * be 2.
2179 */
2180
2181 if(dmabuf->ossmaxfrags > 256)
2182 dmabuf->ossmaxfrags = 256;
2183 else if (dmabuf->ossmaxfrags < 2)
2184 dmabuf->ossmaxfrags = 2;
2185
2186 val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
2187 while (val < 8192) {
2188 val <<= 1;
2189 dmabuf->ossmaxfrags <<= 1;
2190 }
2191 while (val > 65536) {
2192 val >>= 1;
2193 dmabuf->ossmaxfrags >>= 1;
2194 }
2195 dmabuf->ready = 0;
2196#ifdef DEBUG
2197 printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %d\n", val,
2198 dmabuf->ossfragsize, dmabuf->ossmaxfrags);
2199#endif
2200
2201 return 0;
2202
2203 case SNDCTL_DSP_GETOSPACE:
2204 if (!(file->f_mode & FMODE_WRITE))
2205 return -EINVAL;
2206 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2207 return val;
2208 spin_lock_irqsave(&state->card->lock, flags);
2209 i810_update_ptr(state);
2210 abinfo.fragsize = dmabuf->userfragsize;
2211 abinfo.fragstotal = dmabuf->userfrags;
2212 if (dmabuf->mapped)
2213 abinfo.bytes = dmabuf->dmasize;
2214 else
2215 abinfo.bytes = i810_get_free_write_space(state);
2216 abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2217 spin_unlock_irqrestore(&state->card->lock, flags);
2218#if defined(DEBUG) || defined(DEBUG_MMAP)
2219 printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n", abinfo.bytes,
2220 abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
2221#endif
2222 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2223
2224 case SNDCTL_DSP_GETOPTR:
2225 if (!(file->f_mode & FMODE_WRITE))
2226 return -EINVAL;
2227 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2228 return val;
2229 spin_lock_irqsave(&state->card->lock, flags);
2230 val = i810_get_free_write_space(state);
2231 cinfo.bytes = dmabuf->total_bytes;
2232 cinfo.ptr = dmabuf->hwptr;
2233 cinfo.blocks = val/dmabuf->userfragsize;
2234 if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
2235 dmabuf->count += val;
2236 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2237 __i810_update_lvi(state, 0);
2238 }
2239 spin_unlock_irqrestore(&state->card->lock, flags);
2240#if defined(DEBUG) || defined(DEBUG_MMAP)
2241 printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
2242 cinfo.blocks, cinfo.ptr, dmabuf->count);
2243#endif
2244 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2245
2246 case SNDCTL_DSP_GETISPACE:
2247 if (!(file->f_mode & FMODE_READ))
2248 return -EINVAL;
2249 if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
2250 return val;
2251 spin_lock_irqsave(&state->card->lock, flags);
2252 abinfo.bytes = i810_get_available_read_data(state);
2253 abinfo.fragsize = dmabuf->userfragsize;
2254 abinfo.fragstotal = dmabuf->userfrags;
2255 abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2256 spin_unlock_irqrestore(&state->card->lock, flags);
2257#if defined(DEBUG) || defined(DEBUG_MMAP)
2258 printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n", abinfo.bytes,
2259 abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
2260#endif
2261 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2262
2263 case SNDCTL_DSP_GETIPTR:
2264 if (!(file->f_mode & FMODE_READ))
2265 return -EINVAL;
2266 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2267 return val;
2268 spin_lock_irqsave(&state->card->lock, flags);
2269 val = i810_get_available_read_data(state);
2270 cinfo.bytes = dmabuf->total_bytes;
2271 cinfo.blocks = val/dmabuf->userfragsize;
2272 cinfo.ptr = dmabuf->hwptr;
2273 if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
2274 dmabuf->count -= val;
2275 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2276 __i810_update_lvi(state, 1);
2277 }
2278 spin_unlock_irqrestore(&state->card->lock, flags);
2279#if defined(DEBUG) || defined(DEBUG_MMAP)
2280 printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
2281 cinfo.blocks, cinfo.ptr, dmabuf->count);
2282#endif
2283 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2284
2285 case SNDCTL_DSP_NONBLOCK:
2286#ifdef DEBUG
2287 printk("SNDCTL_DSP_NONBLOCK\n");
2288#endif
2289 file->f_flags |= O_NONBLOCK;
2290 return 0;
2291
2292 case SNDCTL_DSP_GETCAPS:
2293#ifdef DEBUG
2294 printk("SNDCTL_DSP_GETCAPS\n");
2295#endif
2296 return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP|DSP_CAP_BIND,
2297 p);
2298
2299 case SNDCTL_DSP_GETTRIGGER:
2300 val = 0;
2301#ifdef DEBUG
2302 printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
2303#endif
2304 return put_user(dmabuf->trigger, p);
2305
2306 case SNDCTL_DSP_SETTRIGGER:
2307 if (get_user(val, p))
2308 return -EFAULT;
2309#if defined(DEBUG) || defined(DEBUG_MMAP)
2310 printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
2311#endif
2312 /* silently ignore invalid PCM_ENABLE_xxx bits,
2313 * like the other drivers do
2314 */
2315 if (!(file->f_mode & FMODE_READ ))
2316 val &= ~PCM_ENABLE_INPUT;
2317 if (!(file->f_mode & FMODE_WRITE ))
2318 val &= ~PCM_ENABLE_OUTPUT;
2319 if((file->f_mode & FMODE_READ) && !(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
2320 stop_adc(state);
2321 }
2322 if((file->f_mode & FMODE_WRITE) && !(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
2323 stop_dac(state);
2324 }
2325 dmabuf->trigger = val;
2326 if((val & PCM_ENABLE_OUTPUT) && !(dmabuf->enable & DAC_RUNNING)) {
2327 if (!dmabuf->write_channel) {
2328 dmabuf->ready = 0;
2329 dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
2330 if (!dmabuf->write_channel)
2331 return -EBUSY;
2332 }
2333 if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
2334 return ret;
2335 if (dmabuf->mapped) {
2336 spin_lock_irqsave(&state->card->lock, flags);
2337 i810_update_ptr(state);
2338 dmabuf->count = 0;
2339 dmabuf->swptr = dmabuf->hwptr;
2340 dmabuf->count = i810_get_free_write_space(state);
2341 dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2342 spin_unlock_irqrestore(&state->card->lock, flags);
2343 }
2344 i810_update_lvi(state, 0);
2345 start_dac(state);
2346 }
2347 if((val & PCM_ENABLE_INPUT) && !(dmabuf->enable & ADC_RUNNING)) {
2348 if (!dmabuf->read_channel) {
2349 dmabuf->ready = 0;
2350 dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
2351 if (!dmabuf->read_channel)
2352 return -EBUSY;
2353 }
2354 if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
2355 return ret;
2356 if (dmabuf->mapped) {
2357 spin_lock_irqsave(&state->card->lock, flags);
2358 i810_update_ptr(state);
2359 dmabuf->swptr = dmabuf->hwptr;
2360 dmabuf->count = 0;
2361 spin_unlock_irqrestore(&state->card->lock, flags);
2362 }
2363 i810_update_lvi(state, 1);
2364 start_adc(state);
2365 }
2366 return 0;
2367
2368 case SNDCTL_DSP_SETDUPLEX:
2369#ifdef DEBUG
2370 printk("SNDCTL_DSP_SETDUPLEX\n");
2371#endif
2372 return -EINVAL;
2373
2374 case SNDCTL_DSP_GETODELAY:
2375 if (!(file->f_mode & FMODE_WRITE))
2376 return -EINVAL;
2377 spin_lock_irqsave(&state->card->lock, flags);
2378 i810_update_ptr(state);
2379 val = dmabuf->count;
2380 spin_unlock_irqrestore(&state->card->lock, flags);
2381#ifdef DEBUG
2382 printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
2383#endif
2384 return put_user(val, p);
2385
2386 case SOUND_PCM_READ_RATE:
2387#ifdef DEBUG
2388 printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
2389#endif
2390 return put_user(dmabuf->rate, p);
2391
2392 case SOUND_PCM_READ_CHANNELS:
2393#ifdef DEBUG
2394 printk("SOUND_PCM_READ_CHANNELS\n");
2395#endif
2396 return put_user(2, p);
2397
2398 case SOUND_PCM_READ_BITS:
2399#ifdef DEBUG
2400 printk("SOUND_PCM_READ_BITS\n");
2401#endif
2402 return put_user(AFMT_S16_LE, p);
2403
2404 case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
2405#ifdef DEBUG
2406 printk("SNDCTL_DSP_SETSPDIF\n");
2407#endif
2408 if (get_user(val, p))
2409 return -EFAULT;
2410
2411 /* Check to make sure the codec supports S/PDIF transmitter */
2412
2413 if((state->card->ac97_features & 4)) {
2414 /* mask out the transmitter speed bits so the user can't set them */
2415 val &= ~0x3000;
2416
2417 /* Add the current transmitter speed bits to the passed value */
2418 ret = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
2419 val |= (ret & 0x3000);
2420
2421 i810_ac97_set(codec, AC97_SPDIF_CONTROL, val);
2422 if(i810_ac97_get(codec, AC97_SPDIF_CONTROL) != val ) {
2423 printk(KERN_ERR "i810_audio: Unable to set S/PDIF configuration to 0x%04x.\n", val);
2424 return -EFAULT;
2425 }
2426 }
2427#ifdef DEBUG
2428 else
2429 printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.\n");
2430#endif
2431 return put_user(val, p);
2432
2433 case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
2434#ifdef DEBUG
2435 printk("SNDCTL_DSP_GETSPDIF\n");
2436#endif
2437 if (get_user(val, p))
2438 return -EFAULT;
2439
2440 /* Check to make sure the codec supports S/PDIF transmitter */
2441
2442 if(!(state->card->ac97_features & 4)) {
2443#ifdef DEBUG
2444 printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.\n");
2445#endif
2446 val = 0;
2447 } else {
2448 val = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
2449 }
2450 //return put_user((val & 0xcfff), p);
2451 return put_user(val, p);
2452
2453 case SNDCTL_DSP_GETCHANNELMASK:
2454#ifdef DEBUG
2455 printk("SNDCTL_DSP_GETCHANNELMASK\n");
2456#endif
2457 if (get_user(val, p))
2458 return -EFAULT;
2459
2460 /* Based on AC'97 DAC support, not ICH hardware */
2461 val = DSP_BIND_FRONT;
2462 if ( state->card->ac97_features & 0x0004 )
2463 val |= DSP_BIND_SPDIF;
2464
2465 if ( state->card->ac97_features & 0x0080 )
2466 val |= DSP_BIND_SURR;
2467 if ( state->card->ac97_features & 0x0140 )
2468 val |= DSP_BIND_CENTER_LFE;
2469
2470 return put_user(val, p);
2471
2472 case SNDCTL_DSP_BIND_CHANNEL:
2473#ifdef DEBUG
2474 printk("SNDCTL_DSP_BIND_CHANNEL\n");
2475#endif
2476 if (get_user(val, p))
2477 return -EFAULT;
2478 if ( val == DSP_BIND_QUERY ) {
2479 val = DSP_BIND_FRONT; /* Always report this as being enabled */
2480 if ( state->card->ac97_status & SPDIF_ON )
2481 val |= DSP_BIND_SPDIF;
2482 else {
2483 if ( state->card->ac97_status & SURR_ON )
2484 val |= DSP_BIND_SURR;
2485 if ( state->card->ac97_status & CENTER_LFE_ON )
2486 val |= DSP_BIND_CENTER_LFE;
2487 }
2488 } else { /* Not a query, set it */
2489 if (!(file->f_mode & FMODE_WRITE))
2490 return -EINVAL;
2491 if ( dmabuf->enable == DAC_RUNNING ) {
2492 stop_dac(state);
2493 }
2494 if ( val & DSP_BIND_SPDIF ) { /* Turn on SPDIF */
2495 /* Ok, this should probably define what slots
2496 * to use. For now, we'll only set it to the
2497 * defaults:
2498 *
2499 * non multichannel codec maps to slots 3&4
2500 * 2 channel codec maps to slots 7&8
2501 * 4 channel codec maps to slots 6&9
2502 * 6 channel codec maps to slots 10&11
2503 *
2504 * there should be some way for the app to
2505 * select the slot assignment.
2506 */
2507
2508 i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, dmabuf->rate );
2509 if ( !(state->card->ac97_status & SPDIF_ON) )
2510 val &= ~DSP_BIND_SPDIF;
2511 } else {
2512 int mask;
2513 int channels;
2514
2515 /* Turn off S/PDIF if it was on */
2516 if ( state->card->ac97_status & SPDIF_ON )
2517 i810_set_spdif_output ( state, -1, 0 );
2518
2519 mask = val & (DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE);
2520 switch (mask) {
2521 case DSP_BIND_FRONT:
2522 channels = 2;
2523 break;
2524 case DSP_BIND_FRONT|DSP_BIND_SURR:
2525 channels = 4;
2526 break;
2527 case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
2528 channels = 6;
2529 break;
2530 default:
2531 val = DSP_BIND_FRONT;
2532 channels = 2;
2533 break;
2534 }
2535 i810_set_dac_channels ( state, channels );
2536
2537 /* check that they really got turned on */
2538 if (!(state->card->ac97_status & SURR_ON))
2539 val &= ~DSP_BIND_SURR;
2540 if (!(state->card->ac97_status & CENTER_LFE_ON))
2541 val &= ~DSP_BIND_CENTER_LFE;
2542 }
2543 }
2544 return put_user(val, p);
2545
2546 case SNDCTL_DSP_MAPINBUF:
2547 case SNDCTL_DSP_MAPOUTBUF:
2548 case SNDCTL_DSP_SETSYNCRO:
2549 case SOUND_PCM_WRITE_FILTER:
2550 case SOUND_PCM_READ_FILTER:
2551#ifdef DEBUG
2552 printk("SNDCTL_* -EINVAL\n");
2553#endif
2554 return -EINVAL;
2555 }
2556 return -EINVAL;
2557}
2558
2559static int i810_open(struct inode *inode, struct file *file)
2560{
2561 int i = 0;
2562 struct i810_card *card = devs;
2563 struct i810_state *state = NULL;
2564 struct dmabuf *dmabuf = NULL;
2565
2566 /* find an avaiable virtual channel (instance of /dev/dsp) */
2567 while (card != NULL) {
2568 /*
2569 * If we are initializing and then fail, card could go
2570 * away unuexpectedly while we are in the for() loop.
2571 * So, check for card on each iteration before we check
2572 * for card->initializing to avoid a possible oops.
2573 * This usually only matters for times when the driver is
2574 * autoloaded by kmod.
2575 */
2576 for (i = 0; i < 50 && card && card->initializing; i++) {
2577 set_current_state(TASK_UNINTERRUPTIBLE);
2578 schedule_timeout(HZ/20);
2579 }
2580 for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
2581 if (card->states[i] == NULL) {
2582 state = card->states[i] = (struct i810_state *)
2583 kzalloc(sizeof(struct i810_state), GFP_KERNEL);
2584 if (state == NULL)
2585 return -ENOMEM;
2586 dmabuf = &state->dmabuf;
2587 goto found_virt;
2588 }
2589 }
2590 card = card->next;
2591 }
2592 /* no more virtual channel avaiable */
2593 if (!state)
2594 return -ENODEV;
2595
2596found_virt:
2597 /* initialize the virtual channel */
2598 state->virt = i;
2599 state->card = card;
2600 state->magic = I810_STATE_MAGIC;
2601 init_waitqueue_head(&dmabuf->wait);
2602 mutex_init(&state->open_mutex);
2603 file->private_data = state;
2604 dmabuf->trigger = 0;
2605
2606 /* allocate hardware channels */
2607 if(file->f_mode & FMODE_READ) {
2608 if((dmabuf->read_channel = card->alloc_rec_pcm_channel(card)) == NULL) {
2609 kfree (card->states[i]);
2610 card->states[i] = NULL;
2611 return -EBUSY;
2612 }
2613 dmabuf->trigger |= PCM_ENABLE_INPUT;
2614 i810_set_adc_rate(state, 8000);
2615 }
2616 if(file->f_mode & FMODE_WRITE) {
2617 if((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
2618 /* make sure we free the record channel allocated above */
2619 if(file->f_mode & FMODE_READ)
2620 card->free_pcm_channel(card,dmabuf->read_channel->num);
2621 kfree (card->states[i]);
2622 card->states[i] = NULL;
2623 return -EBUSY;
2624 }
2625 /* Initialize to 8kHz? What if we don't support 8kHz? */
2626 /* Let's change this to check for S/PDIF stuff */
2627
2628 dmabuf->trigger |= PCM_ENABLE_OUTPUT;
2629 if ( spdif_locked ) {
2630 i810_set_dac_rate(state, spdif_locked);
2631 i810_set_spdif_output(state, AC97_EA_SPSA_3_4, spdif_locked);
2632 } else {
2633 i810_set_dac_rate(state, 8000);
2634 /* Put the ACLink in 2 channel mode by default */
2635 i = I810_IOREADL(card, GLOB_CNT);
2636 I810_IOWRITEL(i & 0xffcfffff, card, GLOB_CNT);
2637 }
2638 }
2639
2640 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
2641 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
2642 /dev/dspW will accept 16-bits sample, but we don't support those so we
2643 set it immediately to stereo and 16bit, which is all we do support */
2644 dmabuf->fmt |= I810_FMT_16BIT | I810_FMT_STEREO;
2645 dmabuf->ossfragsize = 0;
2646 dmabuf->ossmaxfrags = 0;
2647 dmabuf->subdivision = 0;
2648
2649 state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2650
2651 return nonseekable_open(inode, file);
2652}
2653
2654static int i810_release(struct inode *inode, struct file *file)
2655{
2656 struct i810_state *state = (struct i810_state *)file->private_data;
2657 struct i810_card *card = state->card;
2658 struct dmabuf *dmabuf = &state->dmabuf;
2659 unsigned long flags;
2660
2661 lock_kernel();
2662
2663 /* stop DMA state machine and free DMA buffers/channels */
2664 if(dmabuf->trigger & PCM_ENABLE_OUTPUT) {
2665 drain_dac(state, 0);
2666 }
2667 if(dmabuf->trigger & PCM_ENABLE_INPUT) {
2668 stop_adc(state);
2669 }
2670 spin_lock_irqsave(&card->lock, flags);
2671 dealloc_dmabuf(state);
2672 if (file->f_mode & FMODE_WRITE) {
2673 state->card->free_pcm_channel(state->card, dmabuf->write_channel->num);
2674 }
2675 if (file->f_mode & FMODE_READ) {
2676 state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
2677 }
2678
2679 state->card->states[state->virt] = NULL;
2680 kfree(state);
2681 spin_unlock_irqrestore(&card->lock, flags);
2682 unlock_kernel();
2683
2684 return 0;
2685}
2686
2687static /*const*/ struct file_operations i810_audio_fops = {
2688 .owner = THIS_MODULE,
2689 .llseek = no_llseek,
2690 .read = i810_read,
2691 .write = i810_write,
2692 .poll = i810_poll,
2693 .ioctl = i810_ioctl,
2694 .mmap = i810_mmap,
2695 .open = i810_open,
2696 .release = i810_release,
2697};
2698
2699/* Write AC97 codec registers */
2700
2701static u16 i810_ac97_get_mmio(struct ac97_codec *dev, u8 reg)
2702{
2703 struct i810_card *card = dev->private_data;
2704 int count = 100;
2705 u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
2706
2707 while(count-- && (readb(card->iobase_mmio + CAS) & 1))
2708 udelay(1);
2709
2710#ifdef DEBUG_MMIO
2711 {
2712 u16 ans = readw(card->ac97base_mmio + reg_set);
2713 printk(KERN_DEBUG "i810_audio: ac97_get_mmio(%d) -> 0x%04X\n", ((int) reg_set) & 0xffff, (u32) ans);
2714 return ans;
2715 }
2716#else
2717 return readw(card->ac97base_mmio + reg_set);
2718#endif
2719}
2720
2721static u16 i810_ac97_get_io(struct ac97_codec *dev, u8 reg)
2722{
2723 struct i810_card *card = dev->private_data;
2724 int count = 100;
2725 u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
2726
2727 while(count-- && (I810_IOREADB(card, CAS) & 1))
2728 udelay(1);
2729
2730 return inw(card->ac97base + reg_set);
2731}
2732
2733static void i810_ac97_set_mmio(struct ac97_codec *dev, u8 reg, u16 data)
2734{
2735 struct i810_card *card = dev->private_data;
2736 int count = 100;
2737 u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
2738
2739 while(count-- && (readb(card->iobase_mmio + CAS) & 1))
2740 udelay(1);
2741
2742 writew(data, card->ac97base_mmio + reg_set);
2743
2744#ifdef DEBUG_MMIO
2745 printk(KERN_DEBUG "i810_audio: ac97_set_mmio(0x%04X, %d)\n", (u32) data, ((int) reg_set) & 0xffff);
2746#endif
2747}
2748
2749static void i810_ac97_set_io(struct ac97_codec *dev, u8 reg, u16 data)
2750{
2751 struct i810_card *card = dev->private_data;
2752 int count = 100;
2753 u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
2754
2755 while(count-- && (I810_IOREADB(card, CAS) & 1))
2756 udelay(1);
2757
2758 outw(data, card->ac97base + reg_set);
2759}
2760
2761static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg)
2762{
2763 struct i810_card *card = dev->private_data;
2764 u16 ret;
2765
2766 spin_lock(&card->ac97_lock);
2767 if (card->use_mmio) {
2768 ret = i810_ac97_get_mmio(dev, reg);
2769 }
2770 else {
2771 ret = i810_ac97_get_io(dev, reg);
2772 }
2773 spin_unlock(&card->ac97_lock);
2774
2775 return ret;
2776}
2777
2778static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
2779{
2780 struct i810_card *card = dev->private_data;
2781
2782 spin_lock(&card->ac97_lock);
2783 if (card->use_mmio) {
2784 i810_ac97_set_mmio(dev, reg, data);
2785 }
2786 else {
2787 i810_ac97_set_io(dev, reg, data);
2788 }
2789 spin_unlock(&card->ac97_lock);
2790}
2791
2792
2793/* OSS /dev/mixer file operation methods */
2794
2795static int i810_open_mixdev(struct inode *inode, struct file *file)
2796{
2797 int i;
2798 int minor = iminor(inode);
2799 struct i810_card *card = devs;
2800
2801 for (card = devs; card != NULL; card = card->next) {
2802 /*
2803 * If we are initializing and then fail, card could go
2804 * away unuexpectedly while we are in the for() loop.
2805 * So, check for card on each iteration before we check
2806 * for card->initializing to avoid a possible oops.
2807 * This usually only matters for times when the driver is
2808 * autoloaded by kmod.
2809 */
2810 for (i = 0; i < 50 && card && card->initializing; i++) {
2811 set_current_state(TASK_UNINTERRUPTIBLE);
2812 schedule_timeout(HZ/20);
2813 }
2814 for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
2815 if (card->ac97_codec[i] != NULL &&
2816 card->ac97_codec[i]->dev_mixer == minor) {
2817 file->private_data = card->ac97_codec[i];
2818 return nonseekable_open(inode, file);
2819 }
2820 }
2821 return -ENODEV;
2822}
2823
2824static int i810_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
2825 unsigned long arg)
2826{
2827 struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
2828
2829 return codec->mixer_ioctl(codec, cmd, arg);
2830}
2831
2832static /*const*/ struct file_operations i810_mixer_fops = {
2833 .owner = THIS_MODULE,
2834 .llseek = no_llseek,
2835 .ioctl = i810_ioctl_mixdev,
2836 .open = i810_open_mixdev,
2837};
2838
2839/* AC97 codec initialisation. These small functions exist so we don't
2840 duplicate code between module init and apm resume */
2841
2842static inline int i810_ac97_exists(struct i810_card *card, int ac97_number)
2843{
2844 u32 reg = I810_IOREADL(card, GLOB_STA);
2845 switch (ac97_number) {
2846 case 0:
2847 return reg & (1<<8);
2848 case 1:
2849 return reg & (1<<9);
2850 case 2:
2851 return reg & (1<<28);
2852 }
2853 return 0;
2854}
2855
2856static inline int i810_ac97_enable_variable_rate(struct ac97_codec *codec)
2857{
2858 i810_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
2859 i810_ac97_set(codec,AC97_EXTENDED_STATUS,
2860 i810_ac97_get(codec, AC97_EXTENDED_STATUS)|0xE800);
2861
2862 return (i810_ac97_get(codec, AC97_EXTENDED_STATUS)&1);
2863}
2864
2865
2866static int i810_ac97_probe_and_powerup(struct i810_card *card,struct ac97_codec *codec)
2867{
2868 /* Returns 0 on failure */
2869 int i;
2870
2871 if (ac97_probe_codec(codec) == 0) return 0;
2872
2873 /* power it all up */
2874 i810_ac97_set(codec, AC97_POWER_CONTROL,
2875 i810_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
2876
2877 /* wait for analog ready */
2878 for (i=100; i && ((i810_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf); i--)
2879 {
2880 set_current_state(TASK_UNINTERRUPTIBLE);
2881 schedule_timeout(HZ/20);
2882 }
2883 return i;
2884}
2885
2886static int is_new_ich(u16 pci_id)
2887{
2888 switch (pci_id) {
2889 case PCI_DEVICE_ID_INTEL_82801DB_5:
2890 case PCI_DEVICE_ID_INTEL_82801EB_5:
2891 case PCI_DEVICE_ID_INTEL_ESB_5:
2892 case PCI_DEVICE_ID_INTEL_ICH6_18:
2893 return 1;
2894 default:
2895 break;
2896 }
2897
2898 return 0;
2899}
2900
2901static inline int ich_use_mmio(struct i810_card *card)
2902{
2903 return is_new_ich(card->pci_id) && card->use_mmio;
2904}
2905
2906/**
2907 * i810_ac97_power_up_bus - bring up AC97 link
2908 * @card : ICH audio device to power up
2909 *
2910 * Bring up the ACLink AC97 codec bus
2911 */
2912
2913static int i810_ac97_power_up_bus(struct i810_card *card)
2914{
2915 u32 reg = I810_IOREADL(card, GLOB_CNT);
2916 int i;
2917 int primary_codec_id = 0;
2918
2919 if((reg&2)==0) /* Cold required */
2920 reg|=2;
2921 else
2922 reg|=4; /* Warm */
2923
2924 reg&=~8; /* ACLink on */
2925
2926 /* At this point we deassert AC_RESET # */
2927 I810_IOWRITEL(reg , card, GLOB_CNT);
2928
2929 /* We must now allow time for the Codec initialisation.
2930 600mS is the specified time */
2931
2932 for(i=0;i<10;i++)
2933 {
2934 if((I810_IOREADL(card, GLOB_CNT)&4)==0)
2935 break;
2936
2937 set_current_state(TASK_UNINTERRUPTIBLE);
2938 schedule_timeout(HZ/20);
2939 }
2940 if(i==10)
2941 {
2942 printk(KERN_ERR "i810_audio: AC'97 reset failed.\n");
2943 return 0;
2944 }
2945
2946 set_current_state(TASK_UNINTERRUPTIBLE);
2947 schedule_timeout(HZ/2);
2948
2949 /*
2950 * See if the primary codec comes ready. This must happen
2951 * before we start doing DMA stuff
2952 */
2953 /* see i810_ac97_init for the next 10 lines (jsaw) */
2954 if (card->use_mmio)
2955 readw(card->ac97base_mmio);
2956 else
2957 inw(card->ac97base);
2958 if (ich_use_mmio(card)) {
2959 primary_codec_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
2960 printk(KERN_INFO "i810_audio: Primary codec has ID %d\n",
2961 primary_codec_id);
2962 }
2963
2964 if(! i810_ac97_exists(card, primary_codec_id))
2965 {
2966 printk(KERN_INFO "i810_audio: Codec not ready.. wait.. ");
2967 set_current_state(TASK_UNINTERRUPTIBLE);
2968 schedule_timeout(HZ); /* actually 600mS by the spec */
2969
2970 if(i810_ac97_exists(card, primary_codec_id))
2971 printk("OK\n");
2972 else
2973 printk("no response.\n");
2974 }
2975 if (card->use_mmio)
2976 readw(card->ac97base_mmio);
2977 else
2978 inw(card->ac97base);
2979 return 1;
2980}
2981
2982static int __devinit i810_ac97_init(struct i810_card *card)
2983{
2984 int num_ac97 = 0;
2985 int ac97_id;
2986 int total_channels = 0;
2987 int nr_ac97_max = card_cap[card->pci_id_internal].nr_ac97;
2988 struct ac97_codec *codec;
2989 u16 eid;
2990 u32 reg;
2991
2992 if(!i810_ac97_power_up_bus(card)) return 0;
2993
2994 /* Number of channels supported */
2995 /* What about the codec? Just because the ICH supports */
2996 /* multiple channels doesn't mean the codec does. */
2997 /* we'll have to modify this in the codec section below */
2998 /* to reflect what the codec has. */
2999 /* ICH and ICH0 only support 2 channels so don't bother */
3000 /* to check.... */
3001
3002 card->channels = 2;
3003 reg = I810_IOREADL(card, GLOB_STA);
3004 if ( reg & 0x0200000 )
3005 card->channels = 6;
3006 else if ( reg & 0x0100000 )
3007 card->channels = 4;
3008 printk(KERN_INFO "i810_audio: Audio Controller supports %d channels.\n", card->channels);
3009 printk(KERN_INFO "i810_audio: Defaulting to base 2 channel mode.\n");
3010 reg = I810_IOREADL(card, GLOB_CNT);
3011 I810_IOWRITEL(reg & 0xffcfffff, card, GLOB_CNT);
3012
3013 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++)
3014 card->ac97_codec[num_ac97] = NULL;
3015
3016 /*@FIXME I don't know, if I'm playing to safe here... (jsaw) */
3017 if ((nr_ac97_max > 2) && !card->use_mmio) nr_ac97_max = 2;
3018
3019 for (num_ac97 = 0; num_ac97 < nr_ac97_max; num_ac97++) {
3020 /* codec reset */
3021 printk(KERN_INFO "i810_audio: Resetting connection %d\n", num_ac97);
3022 if (card->use_mmio)
3023 readw(card->ac97base_mmio + 0x80*num_ac97);
3024 else
3025 inw(card->ac97base + 0x80*num_ac97);
3026
3027 /* If we have the SDATA_IN Map Register, as on ICH4, we
3028 do not loop thru all possible codec IDs but thru all
3029 possible IO channels. Bit 0:1 of SDM then holds the
3030 last codec ID spoken to.
3031 */
3032 if (ich_use_mmio(card)) {
3033 ac97_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
3034 printk(KERN_INFO "i810_audio: Connection %d with codec id %d\n",
3035 num_ac97, ac97_id);
3036 }
3037 else {
3038 ac97_id = num_ac97;
3039 }
3040
3041 /* The ICH programmer's reference says you should */
3042 /* check the ready status before probing. So we chk */
3043 /* What do we do if it's not ready? Wait and try */
3044 /* again, or abort? */
3045 if (!i810_ac97_exists(card, ac97_id)) {
3046 if(num_ac97 == 0)
3047 printk(KERN_ERR "i810_audio: Primary codec not ready.\n");
3048 }
3049
3050 if ((codec = ac97_alloc_codec()) == NULL)
3051 return -ENOMEM;
3052
3053 /* initialize some basic codec information, other fields will be filled
3054 in ac97_probe_codec */
3055 codec->private_data = card;
3056 codec->id = ac97_id;
3057 card->ac97_id_map[ac97_id] = num_ac97 * 0x80;
3058
3059 if (card->use_mmio) {
3060 codec->codec_read = i810_ac97_get_mmio;
3061 codec->codec_write = i810_ac97_set_mmio;
3062 }
3063 else {
3064 codec->codec_read = i810_ac97_get_io;
3065 codec->codec_write = i810_ac97_set_io;
3066 }
3067
3068 if(!i810_ac97_probe_and_powerup(card,codec)) {
3069 printk(KERN_ERR "i810_audio: timed out waiting for codec %d analog ready.\n", ac97_id);
3070 ac97_release_codec(codec);
3071 break; /* it didn't work */
3072 }
3073 /* Store state information about S/PDIF transmitter */
3074 card->ac97_status = 0;
3075
3076 /* Don't attempt to get eid until powerup is complete */
3077 eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
3078
3079 if(eid==0xFFFF)
3080 {
3081 printk(KERN_WARNING "i810_audio: no codec attached ?\n");
3082 ac97_release_codec(codec);
3083 break;
3084 }
3085
3086 /* Check for an AC97 1.0 soft modem (ID1) */
3087
3088 if(codec->modem)
3089 {
3090 printk(KERN_WARNING "i810_audio: codec %d is a softmodem - skipping.\n", ac97_id);
3091 ac97_release_codec(codec);
3092 continue;
3093 }
3094
3095 card->ac97_features = eid;
3096
3097 /* Now check the codec for useful features to make up for
3098 the dumbness of the 810 hardware engine */
3099
3100 if(!(eid&0x0001))
3101 printk(KERN_WARNING "i810_audio: only 48Khz playback available.\n");
3102 else
3103 {
3104 if(!i810_ac97_enable_variable_rate(codec)) {
3105 printk(KERN_WARNING "i810_audio: Codec refused to allow VRA, using 48Khz only.\n");
3106 card->ac97_features&=~1;
3107 }
3108 }
3109
3110 /* Turn on the amplifier */
3111
3112 codec->codec_write(codec, AC97_POWER_CONTROL,
3113 codec->codec_read(codec, AC97_POWER_CONTROL) & ~0x8000);
3114
3115 /* Determine how many channels the codec(s) support */
3116 /* - The primary codec always supports 2 */
3117 /* - If the codec supports AMAP, surround DACs will */
3118 /* automaticlly get assigned to slots. */
3119 /* * Check for surround DACs and increment if */
3120 /* found. */
3121 /* - Else check if the codec is revision 2.2 */
3122 /* * If surround DACs exist, assign them to slots */
3123 /* and increment channel count. */
3124
3125 /* All of this only applies to ICH2 and above. ICH */
3126 /* and ICH0 only support 2 channels. ICH2 will only */
3127 /* support multiple codecs in a "split audio" config. */
3128 /* as described above. */
3129
3130 /* TODO: Remove all the debugging messages! */
3131
3132 if((eid & 0xc000) == 0) /* primary codec */
3133 total_channels += 2;
3134
3135 if(eid & 0x200) { /* GOOD, AMAP support */
3136 if (eid & 0x0080) /* L/R Surround channels */
3137 total_channels += 2;
3138 if (eid & 0x0140) /* LFE and Center channels */
3139 total_channels += 2;
3140 printk("i810_audio: AC'97 codec %d supports AMAP, total channels = %d\n", ac97_id, total_channels);
3141 } else if (eid & 0x0400) { /* this only works on 2.2 compliant codecs */
3142 eid &= 0xffcf;
3143 if((eid & 0xc000) != 0) {
3144 switch ( total_channels ) {
3145 case 2:
3146 /* Set dsa1, dsa0 to 01 */
3147 eid |= 0x0010;
3148 break;
3149 case 4:
3150 /* Set dsa1, dsa0 to 10 */
3151 eid |= 0x0020;
3152 break;
3153 case 6:
3154 /* Set dsa1, dsa0 to 11 */
3155 eid |= 0x0030;
3156 break;
3157 }
3158 total_channels += 2;
3159 }
3160 i810_ac97_set(codec, AC97_EXTENDED_ID, eid);
3161 eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
3162 printk("i810_audio: AC'97 codec %d, new EID value = 0x%04x\n", ac97_id, eid);
3163 if (eid & 0x0080) /* L/R Surround channels */
3164 total_channels += 2;
3165 if (eid & 0x0140) /* LFE and Center channels */
3166 total_channels += 2;
3167 printk("i810_audio: AC'97 codec %d, DAC map configured, total channels = %d\n", ac97_id, total_channels);
3168 } else {
3169 printk("i810_audio: AC'97 codec %d Unable to map surround DAC's (or DAC's not present), total channels = %d\n", ac97_id, total_channels);
3170 }
3171
3172 if ((codec->dev_mixer = register_sound_mixer(&i810_mixer_fops, -1)) < 0) {
3173 printk(KERN_ERR "i810_audio: couldn't register mixer!\n");
3174 ac97_release_codec(codec);
3175 break;
3176 }
3177
3178 card->ac97_codec[num_ac97] = codec;
3179 }
3180
3181 /* tune up the primary codec */
3182 ac97_tune_hardware(card->pci_dev, ac97_quirks, ac97_quirk);
3183
3184 /* pick the minimum of channels supported by ICHx or codec(s) */
3185 card->channels = (card->channels > total_channels)?total_channels:card->channels;
3186
3187 return num_ac97;
3188}
3189
3190static void __devinit i810_configure_clocking (void)
3191{
3192 struct i810_card *card;
3193 struct i810_state *state;
3194 struct dmabuf *dmabuf;
3195 unsigned int i, offset, new_offset;
3196 unsigned long flags;
3197
3198 card = devs;
3199 /* We could try to set the clocking for multiple cards, but can you even have
3200 * more than one i810 in a machine? Besides, clocking is global, so unless
3201 * someone actually thinks more than one i810 in a machine is possible and
3202 * decides to rewrite that little bit, setting the rate for more than one card
3203 * is a waste of time.
3204 */
3205 if(card != NULL) {
3206 state = card->states[0] = (struct i810_state *)
3207 kzalloc(sizeof(struct i810_state), GFP_KERNEL);
3208 if (state == NULL)
3209 return;
3210 dmabuf = &state->dmabuf;
3211
3212 dmabuf->write_channel = card->alloc_pcm_channel(card);
3213 state->virt = 0;
3214 state->card = card;
3215 state->magic = I810_STATE_MAGIC;
3216 init_waitqueue_head(&dmabuf->wait);
3217 mutex_init(&state->open_mutex);
3218 dmabuf->fmt = I810_FMT_STEREO | I810_FMT_16BIT;
3219 dmabuf->trigger = PCM_ENABLE_OUTPUT;
3220 i810_set_spdif_output(state, -1, 0);
3221 i810_set_dac_channels(state, 2);
3222 i810_set_dac_rate(state, 48000);
3223 if(prog_dmabuf(state, 0) != 0) {
3224 goto config_out_nodmabuf;
3225 }
3226 if(dmabuf->dmasize < 16384) {
3227 goto config_out;
3228 }
3229 dmabuf->count = dmabuf->dmasize;
3230 CIV_TO_LVI(card, dmabuf->write_channel->port, -1);
3231 local_irq_save(flags);
3232 start_dac(state);
3233 offset = i810_get_dma_addr(state, 0);
3234 mdelay(50);
3235 new_offset = i810_get_dma_addr(state, 0);
3236 stop_dac(state);
3237 local_irq_restore(flags);
3238 i = new_offset - offset;
3239#ifdef DEBUG_INTERRUPTS
3240 printk("i810_audio: %d bytes in 50 milliseconds\n", i);
3241#endif
3242 if(i == 0)
3243 goto config_out;
3244 i = i / 4 * 20;
3245 if (i > 48500 || i < 47500) {
3246 clocking = clocking * clocking / i;
3247 printk("i810_audio: setting clocking to %d\n", clocking);
3248 }
3249config_out:
3250 dealloc_dmabuf(state);
3251config_out_nodmabuf:
3252 state->card->free_pcm_channel(state->card,state->dmabuf.write_channel->num);
3253 kfree(state);
3254 card->states[0] = NULL;
3255 }
3256}
3257
3258/* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
3259 until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
3260
3261static int __devinit i810_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
3262{
3263 struct i810_card *card;
3264
3265 if (pci_enable_device(pci_dev))
3266 return -EIO;
3267
3268 if (pci_set_dma_mask(pci_dev, I810_DMA_MASK)) {
3269 printk(KERN_ERR "i810_audio: architecture does not support"
3270 " 32bit PCI busmaster DMA\n");
3271 return -ENODEV;
3272 }
3273
3274 if ((card = kzalloc(sizeof(struct i810_card), GFP_KERNEL)) == NULL) {
3275 printk(KERN_ERR "i810_audio: out of memory\n");
3276 return -ENOMEM;
3277 }
3278
3279 card->initializing = 1;
3280 card->pci_dev = pci_dev;
3281 card->pci_id = pci_id->device;
3282 card->ac97base = pci_resource_start (pci_dev, 0);
3283 card->iobase = pci_resource_start (pci_dev, 1);
3284
3285 if (!(card->ac97base) || !(card->iobase)) {
3286 card->ac97base = 0;
3287 card->iobase = 0;
3288 }
3289
3290 /* if chipset could have mmio capability, check it */
3291 if (card_cap[pci_id->driver_data].flags & CAP_MMIO) {
3292 card->ac97base_mmio_phys = pci_resource_start (pci_dev, 2);
3293 card->iobase_mmio_phys = pci_resource_start (pci_dev, 3);
3294
3295 if ((card->ac97base_mmio_phys) && (card->iobase_mmio_phys)) {
3296 card->use_mmio = 1;
3297 }
3298 else {
3299 card->ac97base_mmio_phys = 0;
3300 card->iobase_mmio_phys = 0;
3301 }
3302 }
3303
3304 if (!(card->use_mmio) && (!(card->iobase) || !(card->ac97base))) {
3305 printk(KERN_ERR "i810_audio: No I/O resources available.\n");
3306 goto out_mem;
3307 }
3308
3309 card->irq = pci_dev->irq;
3310 card->next = devs;
3311 card->magic = I810_CARD_MAGIC;
3312#ifdef CONFIG_PM
3313 card->pm_suspended=0;
3314#endif
3315 spin_lock_init(&card->lock);
3316 spin_lock_init(&card->ac97_lock);
3317 devs = card;
3318
3319 pci_set_master(pci_dev);
3320
3321 printk(KERN_INFO "i810: %s found at IO 0x%04lx and 0x%04lx, "
3322 "MEM 0x%04lx and 0x%04lx, IRQ %d\n",
3323 card_names[pci_id->driver_data],
3324 card->iobase, card->ac97base,
3325 card->ac97base_mmio_phys, card->iobase_mmio_phys,
3326 card->irq);
3327
3328 card->alloc_pcm_channel = i810_alloc_pcm_channel;
3329 card->alloc_rec_pcm_channel = i810_alloc_rec_pcm_channel;
3330 card->alloc_rec_mic_channel = i810_alloc_rec_mic_channel;
3331 card->free_pcm_channel = i810_free_pcm_channel;
3332
3333 if ((card->channel = pci_alloc_consistent(pci_dev,
3334 sizeof(struct i810_channel)*NR_HW_CH, &card->chandma)) == NULL) {
3335 printk(KERN_ERR "i810: cannot allocate channel DMA memory\n");
3336 goto out_mem;
3337 }
3338
3339 { /* We may dispose of this altogether some time soon, so... */
3340 struct i810_channel *cp = card->channel;
3341
3342 cp[0].offset = 0;
3343 cp[0].port = 0x00;
3344 cp[0].num = 0;
3345 cp[1].offset = 0;
3346 cp[1].port = 0x10;
3347 cp[1].num = 1;
3348 cp[2].offset = 0;
3349 cp[2].port = 0x20;
3350 cp[2].num = 2;
3351 }
3352
3353 /* claim our iospace and irq */
3354 if (!request_region(card->iobase, 64, card_names[pci_id->driver_data])) {
3355 printk(KERN_ERR "i810_audio: unable to allocate region %lx\n", card->iobase);
3356 goto out_region1;
3357 }
3358 if (!request_region(card->ac97base, 256, card_names[pci_id->driver_data])) {
3359 printk(KERN_ERR "i810_audio: unable to allocate region %lx\n", card->ac97base);
3360 goto out_region2;
3361 }
3362
3363 if (card->use_mmio) {
3364 if (request_mem_region(card->ac97base_mmio_phys, 512, "ich_audio MMBAR")) {
3365 if ((card->ac97base_mmio = ioremap(card->ac97base_mmio_phys, 512))) { /*@FIXME can ioremap fail? don't know (jsaw) */
3366 if (request_mem_region(card->iobase_mmio_phys, 256, "ich_audio MBBAR")) {
3367 if ((card->iobase_mmio = ioremap(card->iobase_mmio_phys, 256))) {
3368 printk(KERN_INFO "i810: %s mmio at 0x%04lx and 0x%04lx\n",
3369 card_names[pci_id->driver_data],
3370 (unsigned long) card->ac97base_mmio,
3371 (unsigned long) card->iobase_mmio);
3372 }
3373 else {
3374 iounmap(card->ac97base_mmio);
3375 release_mem_region(card->ac97base_mmio_phys, 512);
3376 release_mem_region(card->iobase_mmio_phys, 512);
3377 card->use_mmio = 0;
3378 }
3379 }
3380 else {
3381 iounmap(card->ac97base_mmio);
3382 release_mem_region(card->ac97base_mmio_phys, 512);
3383 card->use_mmio = 0;
3384 }
3385 }
3386 }
3387 else {
3388 card->use_mmio = 0;
3389 }
3390 }
3391
3392 /* initialize AC97 codec and register /dev/mixer */
3393 if (i810_ac97_init(card) <= 0)
3394 goto out_iospace;
3395 pci_set_drvdata(pci_dev, card);
3396
3397 if(clocking == 0) {
3398 clocking = 48000;
3399 i810_configure_clocking();
3400 }
3401
3402 /* register /dev/dsp */
3403 if ((card->dev_audio = register_sound_dsp(&i810_audio_fops, -1)) < 0) {
3404 int i;
3405 printk(KERN_ERR "i810_audio: couldn't register DSP device!\n");
3406 for (i = 0; i < NR_AC97; i++)
3407 if (card->ac97_codec[i] != NULL) {
3408 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
3409 ac97_release_codec(card->ac97_codec[i]);
3410 }
3411 goto out_iospace;
3412 }
3413
3414 if (request_irq(card->irq, &i810_interrupt, IRQF_SHARED,
3415 card_names[pci_id->driver_data], card)) {
3416 printk(KERN_ERR "i810_audio: unable to allocate irq %d\n", card->irq);
3417 goto out_iospace;
3418 }
3419
3420
3421 card->initializing = 0;
3422 return 0;
3423
3424out_iospace:
3425 if (card->use_mmio) {
3426 iounmap(card->ac97base_mmio);
3427 iounmap(card->iobase_mmio);
3428 release_mem_region(card->ac97base_mmio_phys, 512);
3429 release_mem_region(card->iobase_mmio_phys, 256);
3430 }
3431 release_region(card->ac97base, 256);
3432out_region2:
3433 release_region(card->iobase, 64);
3434out_region1:
3435 pci_free_consistent(pci_dev, sizeof(struct i810_channel)*NR_HW_CH,
3436 card->channel, card->chandma);
3437out_mem:
3438 kfree(card);
3439 return -ENODEV;
3440}
3441
3442static void __devexit i810_remove(struct pci_dev *pci_dev)
3443{
3444 int i;
3445 struct i810_card *card = pci_get_drvdata(pci_dev);
3446 /* free hardware resources */
3447 free_irq(card->irq, devs);
3448 release_region(card->iobase, 64);
3449 release_region(card->ac97base, 256);
3450 pci_free_consistent(pci_dev, sizeof(struct i810_channel)*NR_HW_CH,
3451 card->channel, card->chandma);
3452 if (card->use_mmio) {
3453 iounmap(card->ac97base_mmio);
3454 iounmap(card->iobase_mmio);
3455 release_mem_region(card->ac97base_mmio_phys, 512);
3456 release_mem_region(card->iobase_mmio_phys, 256);
3457 }
3458
3459 /* unregister audio devices */
3460 for (i = 0; i < NR_AC97; i++)
3461 if (card->ac97_codec[i] != NULL) {
3462 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
3463 ac97_release_codec(card->ac97_codec[i]);
3464 card->ac97_codec[i] = NULL;
3465 }
3466 unregister_sound_dsp(card->dev_audio);
3467 kfree(card);
3468}
3469
3470#ifdef CONFIG_PM
3471static int i810_pm_suspend(struct pci_dev *dev, pm_message_t pm_state)
3472{
3473 struct i810_card *card = pci_get_drvdata(dev);
3474 struct i810_state *state;
3475 unsigned long flags;
3476 struct dmabuf *dmabuf;
3477 int i,num_ac97;
3478#ifdef DEBUG
3479 printk("i810_audio: i810_pm_suspend called\n");
3480#endif
3481 if(!card) return 0;
3482 spin_lock_irqsave(&card->lock, flags);
3483 card->pm_suspended=1;
3484 for(i=0;i<NR_HW_CH;i++) {
3485 state = card->states[i];
3486 if(!state) continue;
3487 /* this happens only if there are open files */
3488 dmabuf = &state->dmabuf;
3489 if(dmabuf->enable & DAC_RUNNING ||
3490 (dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
3491 state->pm_saved_dac_rate=dmabuf->rate;
3492 stop_dac(state);
3493 } else {
3494 state->pm_saved_dac_rate=0;
3495 }
3496 if(dmabuf->enable & ADC_RUNNING) {
3497 state->pm_saved_adc_rate=dmabuf->rate;
3498 stop_adc(state);
3499 } else {
3500 state->pm_saved_adc_rate=0;
3501 }
3502 dmabuf->ready = 0;
3503 dmabuf->swptr = dmabuf->hwptr = 0;
3504 dmabuf->count = dmabuf->total_bytes = 0;
3505 }
3506
3507 spin_unlock_irqrestore(&card->lock, flags);
3508
3509 /* save mixer settings */
3510 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3511 struct ac97_codec *codec = card->ac97_codec[num_ac97];
3512 if(!codec) continue;
3513 for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
3514 if((supported_mixer(codec,i)) &&
3515 (codec->read_mixer)) {
3516 card->pm_saved_mixer_settings[i][num_ac97]=
3517 codec->read_mixer(codec,i);
3518 }
3519 }
3520 }
3521 pci_save_state(dev); /* XXX do we need this? */
3522 pci_disable_device(dev); /* disable busmastering */
3523 pci_set_power_state(dev,3); /* Zzz. */
3524
3525 return 0;
3526}
3527
3528
3529static int i810_pm_resume(struct pci_dev *dev)
3530{
3531 int num_ac97,i=0;
3532 struct i810_card *card=pci_get_drvdata(dev);
3533 pci_enable_device(dev);
3534 pci_restore_state (dev);
3535
3536 /* observation of a toshiba portege 3440ct suggests that the
3537 hardware has to be more or less completely reinitialized from
3538 scratch after an apm suspend. Works For Me. -dan */
3539
3540 i810_ac97_power_up_bus(card);
3541
3542 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3543 struct ac97_codec *codec = card->ac97_codec[num_ac97];
3544 /* check they haven't stolen the hardware while we were
3545 away */
3546 if(!codec || !i810_ac97_exists(card,num_ac97)) {
3547 if(num_ac97) continue;
3548 else BUG();
3549 }
3550 if(!i810_ac97_probe_and_powerup(card,codec)) BUG();
3551
3552 if((card->ac97_features&0x0001)) {
3553 /* at probe time we found we could do variable
3554 rates, but APM suspend has made it forget
3555 its magical powers */
3556 if(!i810_ac97_enable_variable_rate(codec)) BUG();
3557 }
3558 /* we lost our mixer settings, so restore them */
3559 for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
3560 if(supported_mixer(codec,i)){
3561 int val=card->
3562 pm_saved_mixer_settings[i][num_ac97];
3563 codec->mixer_state[i]=val;
3564 codec->write_mixer(codec,i,
3565 (val & 0xff) ,
3566 ((val >> 8) & 0xff) );
3567 }
3568 }
3569 }
3570
3571 /* we need to restore the sample rate from whatever it was */
3572 for(i=0;i<NR_HW_CH;i++) {
3573 struct i810_state * state=card->states[i];
3574 if(state) {
3575 if(state->pm_saved_adc_rate)
3576 i810_set_adc_rate(state,state->pm_saved_adc_rate);
3577 if(state->pm_saved_dac_rate)
3578 i810_set_dac_rate(state,state->pm_saved_dac_rate);
3579 }
3580 }
3581
3582
3583 card->pm_suspended = 0;
3584
3585 /* any processes that were reading/writing during the suspend
3586 probably ended up here */
3587 for(i=0;i<NR_HW_CH;i++) {
3588 struct i810_state *state = card->states[i];
3589 if(state) wake_up(&state->dmabuf.wait);
3590 }
3591
3592 return 0;
3593}
3594#endif /* CONFIG_PM */
3595
3596MODULE_AUTHOR("The Linux kernel team");
3597MODULE_DESCRIPTION("Intel 810 audio support");
3598MODULE_LICENSE("GPL");
3599module_param(ftsodell, int, 0444);
3600module_param(clocking, uint, 0444);
3601module_param(strict_clocking, int, 0444);
3602module_param(spdif_locked, int, 0444);
3603
3604#define I810_MODULE_NAME "i810_audio"
3605
3606static struct pci_driver i810_pci_driver = {
3607 .name = I810_MODULE_NAME,
3608 .id_table = i810_pci_tbl,
3609 .probe = i810_probe,
3610 .remove = __devexit_p(i810_remove),
3611#ifdef CONFIG_PM
3612 .suspend = i810_pm_suspend,
3613 .resume = i810_pm_resume,
3614#endif /* CONFIG_PM */
3615};
3616
3617
3618static int __init i810_init_module (void)
3619{
3620 int retval;
3621
3622 printk(KERN_INFO "Intel 810 + AC97 Audio, version "
3623 DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
3624
3625 retval = pci_register_driver(&i810_pci_driver);
3626 if (retval)
3627 return retval;
3628
3629 if(ftsodell != 0) {
3630 printk("i810_audio: ftsodell is now a deprecated option.\n");
3631 }
3632 if(spdif_locked > 0 ) {
3633 if(spdif_locked == 32000 || spdif_locked == 44100 || spdif_locked == 48000) {
3634 printk("i810_audio: Enabling S/PDIF at sample rate %dHz.\n", spdif_locked);
3635 } else {
3636 printk("i810_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3637 spdif_locked = 0;
3638 }
3639 }
3640
3641 return 0;
3642}
3643
3644static void __exit i810_cleanup_module (void)
3645{
3646 pci_unregister_driver(&i810_pci_driver);
3647}
3648
3649module_init(i810_init_module);
3650module_exit(i810_cleanup_module);
3651
3652/*
3653Local Variables:
3654c-basic-offset: 8
3655End:
3656*/
diff --git a/sound/oss/via82cxxx_audio.c b/sound/oss/via82cxxx_audio.c
deleted file mode 100644
index f95aa0946758..000000000000
--- a/sound/oss/via82cxxx_audio.c
+++ /dev/null
@@ -1,3616 +0,0 @@
1/*
2 * Support for VIA 82Cxxx Audio Codecs
3 * Copyright 1999,2000 Jeff Garzik
4 *
5 * Updated to support the VIA 8233/8235 audio subsystem
6 * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc
7 *
8 * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
9 * See the "COPYING" file distributed with this software for more info.
10 * NO WARRANTY
11 *
12 * For a list of known bugs (errata) and documentation,
13 * see via-audio.pdf in Documentation/DocBook.
14 * If this documentation does not exist, run "make pdfdocs".
15 */
16
17
18#define VIA_VERSION "1.9.1-ac4-2.5"
19
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/pci.h>
26#include <linux/poison.h>
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/proc_fs.h>
30#include <linux/spinlock.h>
31#include <linux/sound.h>
32#include <linux/poll.h>
33#include <linux/soundcard.h>
34#include <linux/ac97_codec.h>
35#include <linux/ioport.h>
36#include <linux/delay.h>
37#include <linux/dma-mapping.h>
38#include <asm/io.h>
39#include <asm/uaccess.h>
40#include <linux/mutex.h>
41
42#include "sound_config.h"
43#include "dev_table.h"
44#include "mpu401.h"
45
46
47#undef VIA_DEBUG /* define to enable debugging output and checks */
48#ifdef VIA_DEBUG
49/* note: prints function name for you */
50#define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
51#else
52#define DPRINTK(fmt, args...)
53#endif
54
55#undef VIA_NDEBUG /* define to disable lightweight runtime checks */
56#ifdef VIA_NDEBUG
57#define assert(expr)
58#else
59#define assert(expr) \
60 if(!(expr)) { \
61 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
62 #expr,__FILE__,__FUNCTION__,__LINE__); \
63 }
64#endif
65
66#define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
67
68#define MAX_CARDS 1
69
70#define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
71#define VIA_MODULE_NAME "via82cxxx"
72#define PFX VIA_MODULE_NAME ": "
73
74#define VIA_COUNTER_LIMIT 100000
75
76/* size of DMA buffers */
77#define VIA_MAX_BUFFER_DMA_PAGES 32
78
79/* buffering default values in ms */
80#define VIA_DEFAULT_FRAG_TIME 20
81#define VIA_DEFAULT_BUFFER_TIME 500
82
83/* the hardware has a 256 fragment limit */
84#define VIA_MIN_FRAG_NUMBER 2
85#define VIA_MAX_FRAG_NUMBER 128
86
87#define VIA_MAX_FRAG_SIZE PAGE_SIZE
88#define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
89
90
91/* 82C686 function 5 (audio codec) PCI configuration registers */
92#define VIA_ACLINK_STATUS 0x40
93#define VIA_ACLINK_CTRL 0x41
94#define VIA_FUNC_ENABLE 0x42
95#define VIA_PNP_CONTROL 0x43
96#define VIA_FM_NMI_CTRL 0x48
97
98/*
99 * controller base 0 (scatter-gather) registers
100 *
101 * NOTE: Via datasheet lists first channel as "read"
102 * channel and second channel as "write" channel.
103 * I changed the naming of the constants to be more
104 * clear than I felt the datasheet to be.
105 */
106
107#define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
108#define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
109#define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
110#define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
111
112#define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
113#define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
114#define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
115#define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
116
117/* offsets from base */
118#define VIA_PCM_STATUS 0x00
119#define VIA_PCM_CONTROL 0x01
120#define VIA_PCM_TYPE 0x02
121#define VIA_PCM_LEFTVOL 0x02
122#define VIA_PCM_RIGHTVOL 0x03
123#define VIA_PCM_TABLE_ADDR 0x04
124#define VIA_PCM_STOPRATE 0x08 /* 8233+ */
125#define VIA_PCM_BLOCK_COUNT 0x0C
126
127/* XXX unused DMA channel for FM PCM data */
128#define VIA_BASE0_FM_OUT_CHAN 0x20
129#define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
130#define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
131#define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
132
133/* Six channel audio output on 8233 */
134#define VIA_BASE0_MULTI_OUT_CHAN 0x40
135#define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
136#define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
137#define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
138
139#define VIA_BASE0_AC97_CTRL 0x80
140#define VIA_BASE0_SGD_STATUS_SHADOW 0x84
141#define VIA_BASE0_GPI_INT_ENABLE 0x8C
142#define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
143#define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
144#define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
145#define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
146
147/* Newer VIA we need to monitor the low 3 bits of each channel. This
148 mask covers the channels we don't yet use as well
149 */
150
151#define VIA_NEW_INTR_MASK 0x77077777UL
152
153/* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
154#define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
155#define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
156#define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
157#define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
158#define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
159#define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
160#define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
161#define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
162#define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
163#define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
164#define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
165 VIA_IRQ_ON_FLAG | \
166 VIA_IRQ_ON_EOL)
167#define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
168
169/* PCI configuration register bits and masks */
170#define VIA_CR40_AC97_READY 0x01
171#define VIA_CR40_AC97_LOW_POWER 0x02
172#define VIA_CR40_SECONDARY_READY 0x04
173
174#define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
175#define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
176#define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
177#define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
178#define VIA_CR41_VRA 0x08 /* enable variable sample rate */
179#define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
180#define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
181#define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
182#define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
183 VIA_CR41_AC97_WAKEUP | \
184 VIA_CR41_AC97_SDO)
185#define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
186 VIA_CR41_AC97_RESET | \
187 VIA_CR41_VRA | \
188 VIA_CR41_PCM_ENABLE)
189
190#define VIA_CR42_SB_ENABLE 0x01
191#define VIA_CR42_MIDI_ENABLE 0x02
192#define VIA_CR42_FM_ENABLE 0x04
193#define VIA_CR42_GAME_ENABLE 0x08
194#define VIA_CR42_MIDI_IRQMASK 0x40
195#define VIA_CR42_MIDI_PNP 0x80
196
197#define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
198#define VIA_CR44_AC_LINK_ACCESS (1 << 7)
199
200#define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
201
202/* controller base 0 register bitmasks */
203#define VIA_INT_DISABLE_MASK (~(0x01|0x02))
204#define VIA_SGD_STOPPED (1 << 2)
205#define VIA_SGD_PAUSED (1 << 6)
206#define VIA_SGD_ACTIVE (1 << 7)
207#define VIA_SGD_TERMINATE (1 << 6)
208#define VIA_SGD_FLAG (1 << 0)
209#define VIA_SGD_EOL (1 << 1)
210#define VIA_SGD_START (1 << 7)
211
212#define VIA_CR80_FIRST_CODEC 0
213#define VIA_CR80_SECOND_CODEC (1 << 30)
214#define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
215#define VIA_CR80_VALID (1 << 25)
216#define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
217#define VIA_CR80_BUSY (1 << 24)
218#define VIA_CR83_BUSY (1)
219#define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
220#define VIA_CR80_READ (1 << 23)
221#define VIA_CR80_WRITE_MODE 0
222#define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
223
224/* capabilities we announce */
225#ifdef VIA_SUPPORT_MMAP
226#define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
227 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
228#else
229#define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
230 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
231#endif
232
233/* scatter-gather DMA table entry, exactly as passed to hardware */
234struct via_sgd_table {
235 u32 addr;
236 u32 count; /* includes additional VIA_xxx bits also */
237};
238
239#define VIA_EOL (1 << 31)
240#define VIA_FLAG (1 << 30)
241#define VIA_STOP (1 << 29)
242
243
244enum via_channel_states {
245 sgd_stopped = 0,
246 sgd_in_progress = 1,
247};
248
249
250struct via_buffer_pgtbl {
251 dma_addr_t handle;
252 void *cpuaddr;
253};
254
255
256struct via_channel {
257 atomic_t n_frags;
258 atomic_t hw_ptr;
259 wait_queue_head_t wait;
260
261 unsigned int sw_ptr;
262 unsigned int slop_len;
263 unsigned int n_irqs;
264 int bytes;
265
266 unsigned is_active : 1;
267 unsigned is_record : 1;
268 unsigned is_mapped : 1;
269 unsigned is_enabled : 1;
270 unsigned is_multi: 1; /* 8233 6 channel */
271 u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
272 u8 channels; /* Channel count */
273
274 unsigned rate; /* sample rate */
275 unsigned int frag_size;
276 unsigned int frag_number;
277
278 unsigned char intmask;
279
280 volatile struct via_sgd_table *sgtable;
281 dma_addr_t sgt_handle;
282
283 unsigned int page_number;
284 struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
285
286 long iobase;
287
288 const char *name;
289};
290
291
292/* data stored for each chip */
293struct via_info {
294 struct pci_dev *pdev;
295 long baseaddr;
296
297 struct ac97_codec *ac97;
298 spinlock_t ac97_lock;
299 spinlock_t lock;
300 int card_num; /* unique card number, from 0 */
301
302 int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
303
304 unsigned rev_h : 1;
305 unsigned legacy: 1; /* Has legacy ports */
306 unsigned intmask: 1; /* Needs int bits */
307 unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
308 unsigned volume: 1;
309
310 unsigned locked_rate : 1;
311
312 int mixer_vol; /* 8233/35 volume - not yet implemented */
313
314 struct mutex syscall_mutex;
315 struct mutex open_mutex;
316
317 /* The 8233/8235 have 4 DX audio channels, two record and
318 one six channel out. We bind ch_in to DX 1, ch_out to multichannel
319 and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
320 moment */
321
322 struct via_channel ch_in;
323 struct via_channel ch_out;
324 struct via_channel ch_fm;
325
326#ifdef CONFIG_MIDI_VIA82CXXX
327 void *midi_devc;
328 struct address_info midi_info;
329#endif
330};
331
332
333/* number of cards, used for assigning unique numbers to cards */
334static unsigned via_num_cards;
335
336
337
338/****************************************************************
339 *
340 * prototypes
341 *
342 *
343 */
344
345static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
346static void __devexit via_remove_one (struct pci_dev *pdev);
347
348static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
349static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
350static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
351static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
352static int via_dsp_open (struct inode *inode, struct file *file);
353static int via_dsp_release(struct inode *inode, struct file *file);
354static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
355
356static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
357static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
358static u8 via_ac97_wait_idle (struct via_info *card);
359
360static void via_chan_free (struct via_info *card, struct via_channel *chan);
361static void via_chan_clear (struct via_info *card, struct via_channel *chan);
362static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
363static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
364
365
366/****************************************************************
367 *
368 * Various data the driver needs
369 *
370 *
371 */
372
373
374static struct pci_device_id via_pci_tbl[] = {
375 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
376 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
377 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
378 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
379 { 0, }
380};
381MODULE_DEVICE_TABLE(pci,via_pci_tbl);
382
383
384static struct pci_driver via_driver = {
385 .name = VIA_MODULE_NAME,
386 .id_table = via_pci_tbl,
387 .probe = via_init_one,
388 .remove = __devexit_p(via_remove_one),
389};
390
391
392/****************************************************************
393 *
394 * Low-level base 0 register read/write helpers
395 *
396 *
397 */
398
399/**
400 * via_chan_stop - Terminate DMA on specified PCM channel
401 * @iobase: PCI base address for SGD channel registers
402 *
403 * Terminate scatter-gather DMA operation for given
404 * channel (derived from @iobase), if DMA is active.
405 *
406 * Note that @iobase is not the PCI base address,
407 * but the PCI base address plus an offset to
408 * one of three PCM channels supported by the chip.
409 *
410 */
411
412static inline void via_chan_stop (long iobase)
413{
414 if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
415 outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
416}
417
418
419/**
420 * via_chan_status_clear - Clear status flags on specified DMA channel
421 * @iobase: PCI base address for SGD channel registers
422 *
423 * Clear any pending status flags for the given
424 * DMA channel (derived from @iobase), if any
425 * flags are asserted.
426 *
427 * Note that @iobase is not the PCI base address,
428 * but the PCI base address plus an offset to
429 * one of three PCM channels supported by the chip.
430 *
431 */
432
433static inline void via_chan_status_clear (long iobase)
434{
435 u8 tmp = inb (iobase + VIA_PCM_STATUS);
436
437 if (tmp != 0)
438 outb (tmp, iobase + VIA_PCM_STATUS);
439}
440
441
442/**
443 * sg_begin - Begin recording or playback on a PCM channel
444 * @chan: Channel for which DMA operation shall begin
445 *
446 * Start scatter-gather DMA for the given channel.
447 *
448 */
449
450static inline void sg_begin (struct via_channel *chan)
451{
452 DPRINTK("Start with intmask %d\n", chan->intmask);
453 DPRINTK("About to start from %d to %d\n",
454 inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
455 inb(chan->iobase + VIA_PCM_STOPRATE + 3));
456 outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
457 DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
458 DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
459}
460
461
462static int sg_active (long iobase)
463{
464 u8 tmp = inb (iobase + VIA_PCM_STATUS);
465 if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
466 printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
467 return 0;
468 }
469 if (tmp & VIA_SGD_ACTIVE)
470 return 1;
471 return 0;
472}
473
474static int via_sg_offset(struct via_channel *chan)
475{
476 return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
477}
478
479/****************************************************************
480 *
481 * Miscellaneous debris
482 *
483 *
484 */
485
486
487/**
488 * via_syscall_down - down the card-specific syscell semaphore
489 * @card: Private info for specified board
490 * @nonblock: boolean, non-zero if O_NONBLOCK is set
491 *
492 * Encapsulates standard method of acquiring the syscall sem.
493 *
494 * Returns negative errno on error, or zero for success.
495 */
496
497static inline int via_syscall_down (struct via_info *card, int nonblock)
498{
499 /* Thomas Sailer:
500 * EAGAIN is supposed to be used if IO is pending,
501 * not if there is contention on some internal
502 * synchronization primitive which should be
503 * held only for a short time anyway
504 */
505 nonblock = 0;
506
507 if (nonblock) {
508 if (!mutex_trylock(&card->syscall_mutex))
509 return -EAGAIN;
510 } else {
511 if (mutex_lock_interruptible(&card->syscall_mutex))
512 return -ERESTARTSYS;
513 }
514
515 return 0;
516}
517
518
519/**
520 * via_stop_everything - Stop all audio operations
521 * @card: Private info for specified board
522 *
523 * Stops all DMA operations and interrupts, and clear
524 * any pending status bits resulting from those operations.
525 */
526
527static void via_stop_everything (struct via_info *card)
528{
529 u8 tmp, new_tmp;
530
531 DPRINTK ("ENTER\n");
532
533 assert (card != NULL);
534
535 /*
536 * terminate any existing operations on audio read/write channels
537 */
538 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
539 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
540 via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
541 if(card->sixchannel)
542 via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
543
544 /*
545 * clear any existing stops / flags (sanity check mainly)
546 */
547 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
548 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
549 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
550 if(card->sixchannel)
551 via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
552
553 /*
554 * clear any enabled interrupt bits
555 */
556 tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
557 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
558 if (tmp != new_tmp)
559 outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
560
561 tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
562 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
563 if (tmp != new_tmp)
564 outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
565
566 tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
567 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
568 if (tmp != new_tmp)
569 outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
570
571 if(card->sixchannel)
572 {
573 tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
574 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
575 if (tmp != new_tmp)
576 outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
577 }
578
579 udelay(10);
580
581 /*
582 * clear any existing flags
583 */
584 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
585 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
586 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
587
588 DPRINTK ("EXIT\n");
589}
590
591
592/**
593 * via_set_rate - Set PCM rate for given channel
594 * @ac97: Pointer to generic codec info struct
595 * @chan: Private info for specified channel
596 * @rate: Desired PCM sample rate, in Khz
597 *
598 * Sets the PCM sample rate for a channel.
599 *
600 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
601 * due to hardware constraints.
602 */
603
604static int via_set_rate (struct ac97_codec *ac97,
605 struct via_channel *chan, unsigned rate)
606{
607 struct via_info *card = ac97->private_data;
608 int rate_reg;
609 u32 dacp;
610 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
611 u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
612
613 DPRINTK ("ENTER, rate = %d\n", rate);
614
615 if (chan->rate == rate)
616 goto out;
617 if (card->locked_rate) {
618 chan->rate = 48000;
619 goto out;
620 }
621
622 if (rate > 48000) rate = 48000;
623 if (rate < 4000) rate = 4000;
624
625 rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
626 AC97_PCM_FRONT_DAC_RATE;
627
628 /* Save current state */
629 dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
630 mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
631 mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
632 phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
633 pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
634 /* Mute - largely reduces popping */
635 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
636 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
637 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
638 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
639 /* Power down the DAC */
640 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
641
642 /* Set new rate */
643 via_ac97_write_reg (ac97, rate_reg, rate);
644
645 /* Power DAC back up */
646 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
647 udelay (200); /* reduces popping */
648
649 /* Restore volumes */
650 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
651 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
652 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
653 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
654
655 /* the hardware might return a value different than what we
656 * passed to it, so read the rate value back from hardware
657 * to see what we came up with
658 */
659 chan->rate = via_ac97_read_reg (ac97, rate_reg);
660
661 if (chan->rate == 0) {
662 card->locked_rate = 1;
663 chan->rate = 48000;
664 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
665 }
666
667out:
668 DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
669 return chan->rate;
670}
671
672
673/****************************************************************
674 *
675 * Channel-specific operations
676 *
677 *
678 */
679
680
681/**
682 * via_chan_init_defaults - Initialize a struct via_channel
683 * @card: Private audio chip info
684 * @chan: Channel to be initialized
685 *
686 * Zero @chan, and then set all static defaults for the structure.
687 */
688
689static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
690{
691 memset (chan, 0, sizeof (*chan));
692
693 if(card->intmask)
694 chan->intmask = 0x23; /* Turn on the IRQ bits */
695
696 if (chan == &card->ch_out) {
697 chan->name = "PCM-OUT";
698 if(card->sixchannel)
699 {
700 chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
701 chan->is_multi = 1;
702 DPRINTK("Using multichannel for pcm out\n");
703 }
704 else
705 chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
706 } else if (chan == &card->ch_in) {
707 chan->name = "PCM-IN";
708 chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
709 chan->is_record = 1;
710 } else if (chan == &card->ch_fm) {
711 chan->name = "PCM-OUT-FM";
712 chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
713 } else {
714 BUG();
715 }
716
717 init_waitqueue_head (&chan->wait);
718
719 chan->pcm_fmt = VIA_PCM_FMT_MASK;
720 chan->is_enabled = 1;
721
722 chan->frag_number = 0;
723 chan->frag_size = 0;
724 atomic_set(&chan->n_frags, 0);
725 atomic_set (&chan->hw_ptr, 0);
726}
727
728/**
729 * via_chan_init - Initialize PCM channel
730 * @card: Private audio chip info
731 * @chan: Channel to be initialized
732 *
733 * Performs some of the preparations necessary to begin
734 * using a PCM channel.
735 *
736 * Currently the preparations consist of
737 * setting the PCM channel to a known state.
738 */
739
740
741static void via_chan_init (struct via_info *card, struct via_channel *chan)
742{
743
744 DPRINTK ("ENTER\n");
745
746 /* bzero channel structure, and init members to defaults */
747 via_chan_init_defaults (card, chan);
748
749 /* stop any existing channel output */
750 via_chan_clear (card, chan);
751 via_chan_status_clear (chan->iobase);
752 via_chan_pcm_fmt (chan, 1);
753
754 DPRINTK ("EXIT\n");
755}
756
757/**
758 * via_chan_buffer_init - Initialize PCM channel buffer
759 * @card: Private audio chip info
760 * @chan: Channel to be initialized
761 *
762 * Performs some of the preparations necessary to begin
763 * using a PCM channel.
764 *
765 * Currently the preparations include allocating the
766 * scatter-gather DMA table and buffers,
767 * and passing the
768 * address of the DMA table to the hardware.
769 *
770 * Note that special care is taken when passing the
771 * DMA table address to hardware, because it was found
772 * during driver development that the hardware did not
773 * always "take" the address.
774 */
775
776static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
777{
778 int page, offset;
779 int i;
780
781 DPRINTK ("ENTER\n");
782
783
784 chan->intmask = 0;
785 if(card->intmask)
786 chan->intmask = 0x23; /* Turn on the IRQ bits */
787
788 if (chan->sgtable != NULL) {
789 DPRINTK ("EXIT\n");
790 return 0;
791 }
792
793 /* alloc DMA-able memory for scatter-gather table */
794 chan->sgtable = pci_alloc_consistent (card->pdev,
795 (sizeof (struct via_sgd_table) * chan->frag_number),
796 &chan->sgt_handle);
797 if (!chan->sgtable) {
798 printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
799 DPRINTK ("EXIT\n");
800 return -ENOMEM;
801 }
802
803 memset ((void*)chan->sgtable, 0,
804 (sizeof (struct via_sgd_table) * chan->frag_number));
805
806 /* alloc DMA-able memory for scatter-gather buffers */
807
808 chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
809 (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
810
811 for (i = 0; i < chan->page_number; i++) {
812 chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
813 &chan->pgtbl[i].handle);
814
815 if (!chan->pgtbl[i].cpuaddr) {
816 chan->page_number = i;
817 goto err_out_nomem;
818 }
819
820#ifndef VIA_NDEBUG
821 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
822#endif
823
824#if 1
825 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
826 i, (long)chan->pgtbl[i].handle,
827 virt_to_phys(chan->pgtbl[i].cpuaddr),
828 chan->pgtbl[i].cpuaddr);
829#endif
830 }
831
832 for (i = 0; i < chan->frag_number; i++) {
833
834 page = i / (PAGE_SIZE / chan->frag_size);
835 offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
836
837 chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
838 chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
839
840#if 1
841 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
842 i,
843 (long)chan->sgtable[i].addr);
844#endif
845 }
846
847 /* overwrite the last buffer information */
848 chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
849
850 /* set location of DMA-able scatter-gather info table */
851 DPRINTK ("outl (0x%X, 0x%04lX)\n",
852 chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
853
854 via_ac97_wait_idle (card);
855 outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
856 udelay (20);
857 via_ac97_wait_idle (card);
858 /* load no rate adaption, stereo 16bit, set up ring slots */
859 if(card->sixchannel)
860 {
861 if(!chan->is_multi)
862 {
863 outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
864 udelay (20);
865 via_ac97_wait_idle (card);
866 }
867 }
868
869 DPRINTK ("inl (0x%lX) = %x\n",
870 chan->iobase + VIA_PCM_TABLE_ADDR,
871 inl(chan->iobase + VIA_PCM_TABLE_ADDR));
872
873 DPRINTK ("EXIT\n");
874 return 0;
875
876err_out_nomem:
877 printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
878 via_chan_buffer_free (card, chan);
879 DPRINTK ("EXIT\n");
880 return -ENOMEM;
881}
882
883
884/**
885 * via_chan_free - Release a PCM channel
886 * @card: Private audio chip info
887 * @chan: Channel to be released
888 *
889 * Performs all the functions necessary to clean up
890 * an initialized channel.
891 *
892 * Currently these functions include disabled any
893 * active DMA operations, setting the PCM channel
894 * back to a known state, and releasing any allocated
895 * sound buffers.
896 */
897
898static void via_chan_free (struct via_info *card, struct via_channel *chan)
899{
900 DPRINTK ("ENTER\n");
901
902 spin_lock_irq (&card->lock);
903
904 /* stop any existing channel output */
905 via_chan_status_clear (chan->iobase);
906 via_chan_stop (chan->iobase);
907 via_chan_status_clear (chan->iobase);
908
909 spin_unlock_irq (&card->lock);
910
911 synchronize_irq(card->pdev->irq);
912
913 DPRINTK ("EXIT\n");
914}
915
916static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
917{
918 int i;
919
920 DPRINTK ("ENTER\n");
921
922 /* zero location of DMA-able scatter-gather info table */
923 via_ac97_wait_idle(card);
924 outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
925
926 for (i = 0; i < chan->page_number; i++)
927 if (chan->pgtbl[i].cpuaddr) {
928 pci_free_consistent (card->pdev, PAGE_SIZE,
929 chan->pgtbl[i].cpuaddr,
930 chan->pgtbl[i].handle);
931 chan->pgtbl[i].cpuaddr = NULL;
932 chan->pgtbl[i].handle = 0;
933 }
934
935 chan->page_number = 0;
936
937 if (chan->sgtable) {
938 pci_free_consistent (card->pdev,
939 (sizeof (struct via_sgd_table) * chan->frag_number),
940 (void*)chan->sgtable, chan->sgt_handle);
941 chan->sgtable = NULL;
942 }
943
944 DPRINTK ("EXIT\n");
945}
946
947
948/**
949 * via_chan_pcm_fmt - Update PCM channel settings
950 * @chan: Channel to be updated
951 * @reset: Boolean. If non-zero, channel will be reset
952 * to 8-bit mono mode.
953 *
954 * Stores the settings of the current PCM format,
955 * 8-bit or 16-bit, and mono/stereo, into the
956 * hardware settings for the specified channel.
957 * If @reset is non-zero, the channel is reset
958 * to 8-bit mono mode. Otherwise, the channel
959 * is set to the values stored in the channel
960 * information struct @chan.
961 */
962
963static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
964{
965 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
966 chan->pcm_fmt, reset ? "yes" : "no");
967
968 assert (chan != NULL);
969
970 if (reset)
971 {
972 /* reset to 8-bit mono mode */
973 chan->pcm_fmt = 0;
974 chan->channels = 1;
975 }
976
977 /* enable interrupts on FLAG and EOL */
978 chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
979
980 /* if we are recording, enable recording fifo bit */
981 if (chan->is_record)
982 chan->pcm_fmt |= VIA_PCM_REC_FIFO;
983 /* set interrupt select bits where applicable (PCM in & out channels) */
984 if (!chan->is_record)
985 chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
986
987 DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
988
989 if(chan->intmask)
990 {
991 u32 m;
992
993 /*
994 * Channel 0x4 is up to 6 x 16bit and has to be
995 * programmed differently
996 */
997
998 if(chan->is_multi)
999 {
1000 u8 c = 0;
1001
1002 /*
1003 * Load the type bit for num channels
1004 * and 8/16bit
1005 */
1006
1007 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1008 c = 1 << 7;
1009 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1010 c |= (2<<4);
1011 else
1012 c |= (1<<4);
1013
1014 outb(c, chan->iobase + VIA_PCM_TYPE);
1015
1016 /*
1017 * Set the channel steering
1018 * Mono
1019 * Channel 0 to slot 3
1020 * Channel 0 to slot 4
1021 * Stereo
1022 * Channel 0 to slot 3
1023 * Channel 1 to slot 4
1024 */
1025
1026 switch(chan->channels)
1027 {
1028 case 1:
1029 outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1030 break;
1031 case 2:
1032 outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1033 break;
1034 case 4:
1035 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1036 break;
1037 case 6:
1038 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1039 break;
1040 }
1041 }
1042 else
1043 {
1044 /*
1045 * New style, turn off channel volume
1046 * control, set bits in the right register
1047 */
1048 outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
1049 outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
1050
1051 m = inl(chan->iobase + VIA_PCM_STOPRATE);
1052 m &= ~(3<<20);
1053 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1054 m |= (1 << 20);
1055 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1056 m |= (1 << 21);
1057 outl(m, chan->iobase + VIA_PCM_STOPRATE);
1058 }
1059 }
1060 else
1061 outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1062
1063
1064 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1065 chan->pcm_fmt,
1066 inb (chan->iobase + VIA_PCM_TYPE));
1067}
1068
1069
1070/**
1071 * via_chan_clear - Stop DMA channel operation, and reset pointers
1072 * @card: the chip to accessed
1073 * @chan: Channel to be cleared
1074 *
1075 * Call via_chan_stop to halt DMA operations, and then resets
1076 * all software pointers which track DMA operation.
1077 */
1078
1079static void via_chan_clear (struct via_info *card, struct via_channel *chan)
1080{
1081 DPRINTK ("ENTER\n");
1082 via_chan_stop (chan->iobase);
1083 via_chan_buffer_free(card, chan);
1084 chan->is_active = 0;
1085 chan->is_mapped = 0;
1086 chan->is_enabled = 1;
1087 chan->slop_len = 0;
1088 chan->sw_ptr = 0;
1089 chan->n_irqs = 0;
1090 atomic_set (&chan->hw_ptr, 0);
1091 DPRINTK ("EXIT\n");
1092}
1093
1094
1095/**
1096 * via_chan_set_speed - Set PCM sample rate for given channel
1097 * @card: Private info for specified board
1098 * @chan: Channel whose sample rate will be adjusted
1099 * @val: New sample rate, in Khz
1100 *
1101 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
1102 * demand that all audio operations halt (if they are not already
1103 * halted) when the %SNDCTL_DSP_SPEED is given.
1104 *
1105 * This function halts all audio operations for the given channel
1106 * @chan, and then calls via_set_rate to set the audio hardware
1107 * to the new rate.
1108 */
1109
1110static int via_chan_set_speed (struct via_info *card,
1111 struct via_channel *chan, int val)
1112{
1113 DPRINTK ("ENTER, requested rate = %d\n", val);
1114
1115 via_chan_clear (card, chan);
1116
1117 val = via_set_rate (card->ac97, chan, val);
1118
1119 DPRINTK ("EXIT, returning %d\n", val);
1120 return val;
1121}
1122
1123
1124/**
1125 * via_chan_set_fmt - Set PCM sample size for given channel
1126 * @card: Private info for specified board
1127 * @chan: Channel whose sample size will be adjusted
1128 * @val: New sample size, use the %AFMT_xxx constants
1129 *
1130 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
1131 * demand that all audio operations halt (if they are not already
1132 * halted) when the %SNDCTL_DSP_SETFMT is given.
1133 *
1134 * This function halts all audio operations for the given channel
1135 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1136 * to the new sample size, either 8-bit or 16-bit.
1137 */
1138
1139static int via_chan_set_fmt (struct via_info *card,
1140 struct via_channel *chan, int val)
1141{
1142 DPRINTK ("ENTER, val=%s\n",
1143 val == AFMT_U8 ? "AFMT_U8" :
1144 val == AFMT_S16_LE ? "AFMT_S16_LE" :
1145 "unknown");
1146
1147 via_chan_clear (card, chan);
1148
1149 assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1150
1151 switch (val) {
1152 case AFMT_S16_LE:
1153 if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
1154 chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
1155 via_chan_pcm_fmt (chan, 0);
1156 }
1157 break;
1158
1159 case AFMT_U8:
1160 if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1161 chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1162 via_chan_pcm_fmt (chan, 0);
1163 }
1164 break;
1165
1166 default:
1167 DPRINTK ("unknown AFMT: 0x%X\n", val);
1168 val = AFMT_S16_LE;
1169 }
1170
1171 DPRINTK ("EXIT\n");
1172 return val;
1173}
1174
1175
1176/**
1177 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
1178 * @card: Private info for specified board
1179 * @chan: Channel whose stereo setting will be adjusted
1180 * @val: New sample size, use the %AFMT_xxx constants
1181 *
1182 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
1183 * demand that all audio operations halt (if they are not already
1184 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1185 *
1186 * This function halts all audio operations for the given channel
1187 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1188 * to enable or disable stereo.
1189 */
1190
1191static int via_chan_set_stereo (struct via_info *card,
1192 struct via_channel *chan, int val)
1193{
1194 DPRINTK ("ENTER, channels = %d\n", val);
1195
1196 via_chan_clear (card, chan);
1197
1198 switch (val) {
1199
1200 /* mono */
1201 case 1:
1202 chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1203 chan->channels = 1;
1204 via_chan_pcm_fmt (chan, 0);
1205 break;
1206
1207 /* stereo */
1208 case 2:
1209 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1210 chan->channels = 2;
1211 via_chan_pcm_fmt (chan, 0);
1212 break;
1213
1214 case 4:
1215 case 6:
1216 if(chan->is_multi)
1217 {
1218 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1219 chan->channels = val;
1220 break;
1221 }
1222 /* unknown */
1223 default:
1224 val = -EINVAL;
1225 break;
1226 }
1227
1228 DPRINTK ("EXIT, returning %d\n", val);
1229 return val;
1230}
1231
1232static int via_chan_set_buffering (struct via_info *card,
1233 struct via_channel *chan, int val)
1234{
1235 int shift;
1236
1237 DPRINTK ("ENTER\n");
1238
1239 /* in both cases the buffer cannot be changed */
1240 if (chan->is_active || chan->is_mapped) {
1241 DPRINTK ("EXIT\n");
1242 return -EINVAL;
1243 }
1244
1245 /* called outside SETFRAGMENT */
1246 /* set defaults or do nothing */
1247 if (val < 0) {
1248
1249 if (chan->frag_size && chan->frag_number)
1250 goto out;
1251
1252 DPRINTK ("\n");
1253
1254 chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1255 * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1256
1257 shift = 0;
1258 while (chan->frag_size) {
1259 chan->frag_size >>= 1;
1260 shift++;
1261 }
1262 chan->frag_size = 1 << shift;
1263
1264 chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
1265
1266 DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
1267 } else {
1268 chan->frag_size = 1 << (val & 0xFFFF);
1269 chan->frag_number = (val >> 16) & 0xFFFF;
1270
1271 DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
1272 }
1273
1274 /* quake3 wants frag_number to be a power of two */
1275 shift = 0;
1276 while (chan->frag_number) {
1277 chan->frag_number >>= 1;
1278 shift++;
1279 }
1280 chan->frag_number = 1 << shift;
1281
1282 if (chan->frag_size > VIA_MAX_FRAG_SIZE)
1283 chan->frag_size = VIA_MAX_FRAG_SIZE;
1284 else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
1285 chan->frag_size = VIA_MIN_FRAG_SIZE;
1286
1287 if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
1288 chan->frag_number = VIA_MIN_FRAG_NUMBER;
1289 if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
1290 chan->frag_number = VIA_MAX_FRAG_NUMBER;
1291
1292 if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
1293 chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
1294
1295out:
1296 if (chan->is_record)
1297 atomic_set (&chan->n_frags, 0);
1298 else
1299 atomic_set (&chan->n_frags, chan->frag_number);
1300
1301 DPRINTK ("EXIT\n");
1302
1303 return 0;
1304}
1305
1306#ifdef VIA_CHAN_DUMP_BUFS
1307/**
1308 * via_chan_dump_bufs - Display DMA table contents
1309 * @chan: Channel whose DMA table will be displayed
1310 *
1311 * Debugging function which displays the contents of the
1312 * scatter-gather DMA table for the given channel @chan.
1313 */
1314
1315static void via_chan_dump_bufs (struct via_channel *chan)
1316{
1317 int i;
1318
1319 for (i = 0; i < chan->frag_number; i++) {
1320 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1321 i, chan->sgtable[i].addr,
1322 chan->sgtable[i].count & 0x00FFFFFF,
1323 chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
1324 chan->sgtable[i].count & VIA_EOL ? 1 : 0);
1325 }
1326 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1327 atomic_read (&chan->buf_in_use),
1328 atomic_read (&chan->sw_ptr));
1329}
1330#endif /* VIA_CHAN_DUMP_BUFS */
1331
1332
1333/**
1334 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1335 * @chan: Channel whose DMA table will be flushed
1336 *
1337 * Flushes partially-full playback buffer to hardware.
1338 */
1339
1340static void via_chan_flush_frag (struct via_channel *chan)
1341{
1342 DPRINTK ("ENTER\n");
1343
1344 assert (chan->slop_len > 0);
1345
1346 if (chan->sw_ptr == (chan->frag_number - 1))
1347 chan->sw_ptr = 0;
1348 else
1349 chan->sw_ptr++;
1350
1351 chan->slop_len = 0;
1352
1353 assert (atomic_read (&chan->n_frags) > 0);
1354 atomic_dec (&chan->n_frags);
1355
1356 DPRINTK ("EXIT\n");
1357}
1358
1359
1360
1361/**
1362 * via_chan_maybe_start - Initiate audio hardware DMA operation
1363 * @chan: Channel whose DMA is to be started
1364 *
1365 * Initiate DMA operation, if the DMA engine for the given
1366 * channel @chan is not already active.
1367 */
1368
1369static inline void via_chan_maybe_start (struct via_channel *chan)
1370{
1371 assert (chan->is_active == sg_active(chan->iobase));
1372
1373 DPRINTK ("MAYBE START %s\n", chan->name);
1374 if (!chan->is_active && chan->is_enabled) {
1375 chan->is_active = 1;
1376 sg_begin (chan);
1377 DPRINTK ("starting channel %s\n", chan->name);
1378 }
1379}
1380
1381
1382/****************************************************************
1383 *
1384 * Interface to ac97-codec module
1385 *
1386 *
1387 */
1388
1389/**
1390 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1391 * @card: Private info for specified board
1392 *
1393 * Sleep until the AC97 codec is no longer busy.
1394 * Returns the final value read from the SGD
1395 * register being polled.
1396 */
1397
1398static u8 via_ac97_wait_idle (struct via_info *card)
1399{
1400 u8 tmp8;
1401 int counter = VIA_COUNTER_LIMIT;
1402
1403 DPRINTK ("ENTER/EXIT\n");
1404
1405 assert (card != NULL);
1406 assert (card->pdev != NULL);
1407
1408 do {
1409 udelay (15);
1410
1411 tmp8 = inb (card->baseaddr + 0x83);
1412 } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
1413
1414 if (tmp8 & VIA_CR83_BUSY)
1415 printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
1416 return tmp8;
1417}
1418
1419
1420/**
1421 * via_ac97_read_reg - Read AC97 standard register
1422 * @codec: Pointer to generic AC97 codec info
1423 * @reg: Index of AC97 register to be read
1424 *
1425 * Read the value of a single AC97 codec register,
1426 * as defined by the Intel AC97 specification.
1427 *
1428 * Defines the standard AC97 read-register operation
1429 * required by the kernel's ac97_codec interface.
1430 *
1431 * Returns the 16-bit value stored in the specified
1432 * register.
1433 */
1434
1435static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1436{
1437 unsigned long data;
1438 struct via_info *card;
1439 int counter;
1440
1441 DPRINTK ("ENTER\n");
1442
1443 assert (codec != NULL);
1444 assert (codec->private_data != NULL);
1445
1446 card = codec->private_data;
1447
1448 spin_lock(&card->ac97_lock);
1449
1450 /* Every time we write to register 80 we cause a transaction.
1451 The only safe way to clear the valid bit is to write it at
1452 the same time as the command */
1453 data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
1454
1455 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1456 udelay (20);
1457
1458 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1459 udelay (1);
1460 if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1461 (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1462 goto out;
1463 }
1464
1465 printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1466 goto err_out;
1467
1468out:
1469 /* Once the valid bit has become set, we must wait a complete AC97
1470 frame before the data has settled. */
1471 udelay(25);
1472 data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
1473
1474 outb (0x02, card->baseaddr + 0x83);
1475
1476 if (((data & 0x007F0000) >> 16) == reg) {
1477 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1478 data, data & 0x0000FFFF);
1479 spin_unlock(&card->ac97_lock);
1480 return data & 0x0000FFFF;
1481 }
1482
1483 printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1484 reg, ((data & 0x007F0000) >> 16));
1485
1486err_out:
1487 spin_unlock(&card->ac97_lock);
1488 DPRINTK ("EXIT, returning 0\n");
1489 return 0;
1490}
1491
1492
1493/**
1494 * via_ac97_write_reg - Write AC97 standard register
1495 * @codec: Pointer to generic AC97 codec info
1496 * @reg: Index of AC97 register to be written
1497 * @value: Value to be written to AC97 register
1498 *
1499 * Write the value of a single AC97 codec register,
1500 * as defined by the Intel AC97 specification.
1501 *
1502 * Defines the standard AC97 write-register operation
1503 * required by the kernel's ac97_codec interface.
1504 */
1505
1506static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
1507{
1508 u32 data;
1509 struct via_info *card;
1510 int counter;
1511
1512 DPRINTK ("ENTER\n");
1513
1514 assert (codec != NULL);
1515 assert (codec->private_data != NULL);
1516
1517 card = codec->private_data;
1518
1519 spin_lock(&card->ac97_lock);
1520
1521 data = (reg << 16) + value;
1522 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1523 udelay (10);
1524
1525 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1526 if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1527 goto out;
1528
1529 udelay (15);
1530 }
1531
1532 printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1533
1534out:
1535 spin_unlock(&card->ac97_lock);
1536 DPRINTK ("EXIT\n");
1537}
1538
1539
1540static int via_mixer_open (struct inode *inode, struct file *file)
1541{
1542 int minor = iminor(inode);
1543 struct via_info *card;
1544 struct pci_dev *pdev = NULL;
1545 struct pci_driver *drvr;
1546
1547 DPRINTK ("ENTER\n");
1548
1549 while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
1550 drvr = pci_dev_driver (pdev);
1551 if (drvr == &via_driver) {
1552 assert (pci_get_drvdata (pdev) != NULL);
1553
1554 card = pci_get_drvdata (pdev);
1555 if (card->ac97->dev_mixer == minor)
1556 goto match;
1557 }
1558 }
1559
1560 DPRINTK ("EXIT, returning -ENODEV\n");
1561 return -ENODEV;
1562
1563match:
1564 pci_dev_put(pdev);
1565 file->private_data = card->ac97;
1566
1567 DPRINTK ("EXIT, returning 0\n");
1568 return nonseekable_open(inode, file);
1569}
1570
1571static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
1572 unsigned long arg)
1573{
1574 struct ac97_codec *codec = file->private_data;
1575 struct via_info *card;
1576 int nonblock = (file->f_flags & O_NONBLOCK);
1577 int rc;
1578
1579 DPRINTK ("ENTER\n");
1580
1581 assert (codec != NULL);
1582 card = codec->private_data;
1583 assert (card != NULL);
1584
1585 rc = via_syscall_down (card, nonblock);
1586 if (rc) goto out;
1587
1588#if 0
1589 /*
1590 * Intercept volume control on 8233 and 8235
1591 */
1592 if(card->volume)
1593 {
1594 switch(cmd)
1595 {
1596 case SOUND_MIXER_READ_VOLUME:
1597 return card->mixer_vol;
1598 case SOUND_MIXER_WRITE_VOLUME:
1599 {
1600 int v;
1601 if(get_user(v, (int *)arg))
1602 {
1603 rc = -EFAULT;
1604 goto out;
1605 }
1606 card->mixer_vol = v;
1607 }
1608 }
1609 }
1610#endif
1611 rc = codec->mixer_ioctl(codec, cmd, arg);
1612
1613 mutex_unlock(&card->syscall_mutex);
1614
1615out:
1616 DPRINTK ("EXIT, returning %d\n", rc);
1617 return rc;
1618}
1619
1620
1621static const struct file_operations via_mixer_fops = {
1622 .owner = THIS_MODULE,
1623 .open = via_mixer_open,
1624 .llseek = no_llseek,
1625 .ioctl = via_mixer_ioctl,
1626};
1627
1628
1629static int __devinit via_ac97_reset (struct via_info *card)
1630{
1631 struct pci_dev *pdev = card->pdev;
1632 u8 tmp8;
1633 u16 tmp16;
1634
1635 DPRINTK ("ENTER\n");
1636
1637 assert (pdev != NULL);
1638
1639#ifndef NDEBUG
1640 {
1641 u8 r40,r41,r42,r43,r44,r48;
1642 pci_read_config_byte (card->pdev, 0x40, &r40);
1643 pci_read_config_byte (card->pdev, 0x41, &r41);
1644 pci_read_config_byte (card->pdev, 0x42, &r42);
1645 pci_read_config_byte (card->pdev, 0x43, &r43);
1646 pci_read_config_byte (card->pdev, 0x44, &r44);
1647 pci_read_config_byte (card->pdev, 0x48, &r48);
1648 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1649 r40,r41,r42,r43,r44,r48);
1650
1651 spin_lock_irq (&card->lock);
1652 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1653 inb (card->baseaddr + 0x00),
1654 inb (card->baseaddr + 0x01),
1655 inb (card->baseaddr + 0x02),
1656 inl (card->baseaddr + 0x04),
1657 inl (card->baseaddr + 0x0C),
1658 inl (card->baseaddr + 0x80),
1659 inl (card->baseaddr + 0x84));
1660 spin_unlock_irq (&card->lock);
1661
1662 }
1663#endif
1664
1665 /*
1666 * Reset AC97 controller: enable, disable, enable,
1667 * pausing after each command for good luck. Only
1668 * do this if the codec is not ready, because it causes
1669 * loud pops and such due to such a hard codec reset.
1670 */
1671 pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
1672 if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
1673 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1674 VIA_CR41_AC97_ENABLE |
1675 VIA_CR41_AC97_RESET |
1676 VIA_CR41_AC97_WAKEUP);
1677 udelay (100);
1678
1679 pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
1680 udelay (100);
1681
1682 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1683 VIA_CR41_AC97_ENABLE |
1684 VIA_CR41_PCM_ENABLE |
1685 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1686 udelay (100);
1687 }
1688
1689 /* Make sure VRA is enabled, in case we didn't do a
1690 * complete codec reset, above
1691 */
1692 pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
1693 if (((tmp8 & VIA_CR41_VRA) == 0) ||
1694 ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
1695 ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
1696 ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
1697 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1698 VIA_CR41_AC97_ENABLE |
1699 VIA_CR41_PCM_ENABLE |
1700 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1701 udelay (100);
1702 }
1703
1704 if(card->legacy)
1705 {
1706#if 0 /* this breaks on K7M */
1707 /* disable legacy stuff */
1708 pci_write_config_byte (pdev, 0x42, 0x00);
1709 udelay(10);
1710#endif
1711
1712 /* route FM trap to IRQ, disable FM trap */
1713 pci_write_config_byte (pdev, 0x48, 0x05);
1714 udelay(10);
1715 }
1716
1717 /* disable all codec GPI interrupts */
1718 outl (0, pci_resource_start (pdev, 0) + 0x8C);
1719
1720 /* WARNING: this line is magic. Remove this
1721 * and things break. */
1722 /* enable variable rate */
1723 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1724 if ((tmp16 & 1) == 0)
1725 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1726
1727 DPRINTK ("EXIT, returning 0\n");
1728 return 0;
1729}
1730
1731
1732static void via_ac97_codec_wait (struct ac97_codec *codec)
1733{
1734 assert (codec->private_data != NULL);
1735 via_ac97_wait_idle (codec->private_data);
1736}
1737
1738
1739static int __devinit via_ac97_init (struct via_info *card)
1740{
1741 int rc;
1742 u16 tmp16;
1743
1744 DPRINTK ("ENTER\n");
1745
1746 assert (card != NULL);
1747
1748 card->ac97 = ac97_alloc_codec();
1749 if(card->ac97 == NULL)
1750 return -ENOMEM;
1751
1752 card->ac97->private_data = card;
1753 card->ac97->codec_read = via_ac97_read_reg;
1754 card->ac97->codec_write = via_ac97_write_reg;
1755 card->ac97->codec_wait = via_ac97_codec_wait;
1756
1757 card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
1758 if (card->ac97->dev_mixer < 0) {
1759 printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
1760 DPRINTK ("EXIT, returning -EIO\n");
1761 ac97_release_codec(card->ac97);
1762 return -EIO;
1763 }
1764
1765 rc = via_ac97_reset (card);
1766 if (rc) {
1767 printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1768 goto err_out;
1769 }
1770
1771 mdelay(10);
1772
1773 if (ac97_probe_codec (card->ac97) == 0) {
1774 printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1775 rc = -EIO;
1776 goto err_out;
1777 }
1778
1779 /* enable variable rate */
1780 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1781 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1782
1783 /*
1784 * If we cannot enable VRA, we have a locked-rate codec.
1785 * We try again to enable VRA before assuming so, however.
1786 */
1787 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1788 if ((tmp16 & 1) == 0) {
1789 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1790 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1791 if ((tmp16 & 1) == 0) {
1792 card->locked_rate = 1;
1793 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
1794 }
1795 }
1796
1797 DPRINTK ("EXIT, returning 0\n");
1798 return 0;
1799
1800err_out:
1801 unregister_sound_mixer (card->ac97->dev_mixer);
1802 DPRINTK ("EXIT, returning %d\n", rc);
1803 ac97_release_codec(card->ac97);
1804 return rc;
1805}
1806
1807
1808static void via_ac97_cleanup (struct via_info *card)
1809{
1810 DPRINTK ("ENTER\n");
1811
1812 assert (card != NULL);
1813 assert (card->ac97->dev_mixer >= 0);
1814
1815 unregister_sound_mixer (card->ac97->dev_mixer);
1816 ac97_release_codec(card->ac97);
1817
1818 DPRINTK ("EXIT\n");
1819}
1820
1821
1822
1823/****************************************************************
1824 *
1825 * Interrupt-related code
1826 *
1827 */
1828
1829/**
1830 * via_intr_channel - handle an interrupt for a single channel
1831 * @card: unused
1832 * @chan: handle interrupt for this channel
1833 *
1834 * This is the "meat" of the interrupt handler,
1835 * containing the actions taken each time an interrupt
1836 * occurs. All communication and coordination with
1837 * userspace takes place here.
1838 *
1839 * Locking: inside card->lock
1840 */
1841
1842static void via_intr_channel (struct via_info *card, struct via_channel *chan)
1843{
1844 u8 status;
1845 int n;
1846
1847 /* check pertinent bits of status register for action bits */
1848 status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1849 if (!status)
1850 return;
1851
1852 /* acknowledge any flagged bits ASAP */
1853 outb (status, chan->iobase);
1854
1855 if (!chan->sgtable) /* XXX: temporary solution */
1856 return;
1857
1858 /* grab current h/w ptr value */
1859 n = atomic_read (&chan->hw_ptr);
1860
1861 /* sanity check: make sure our h/w ptr doesn't have a weird value */
1862 assert (n >= 0);
1863 assert (n < chan->frag_number);
1864
1865
1866 /* reset SGD data structure in memory to reflect a full buffer,
1867 * and advance the h/w ptr, wrapping around to zero if needed
1868 */
1869 if (n == (chan->frag_number - 1)) {
1870 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
1871 atomic_set (&chan->hw_ptr, 0);
1872 } else {
1873 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
1874 atomic_inc (&chan->hw_ptr);
1875 }
1876
1877 /* accounting crap for SNDCTL_DSP_GETxPTR */
1878 chan->n_irqs++;
1879 chan->bytes += chan->frag_size;
1880 /* FIXME - signed overflow is undefined */
1881 if (chan->bytes < 0) /* handle overflow of 31-bit value */
1882 chan->bytes = chan->frag_size;
1883 /* all following checks only occur when not in mmap(2) mode */
1884 if (!chan->is_mapped)
1885 {
1886 /* If we are recording, then n_frags represents the number
1887 * of fragments waiting to be handled by userspace.
1888 * If we are playback, then n_frags represents the number
1889 * of fragments remaining to be filled by userspace.
1890 * We increment here. If we reach max number of fragments,
1891 * this indicates an underrun/overrun. For this case under OSS,
1892 * we stop the record/playback process.
1893 */
1894 if (atomic_read (&chan->n_frags) < chan->frag_number)
1895 atomic_inc (&chan->n_frags);
1896 assert (atomic_read (&chan->n_frags) <= chan->frag_number);
1897 if (atomic_read (&chan->n_frags) == chan->frag_number) {
1898 chan->is_active = 0;
1899 via_chan_stop (chan->iobase);
1900 }
1901 }
1902 /* wake up anyone listening to see when interrupts occur */
1903 wake_up_all (&chan->wait);
1904
1905 DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1906 chan->name, status, (long) inl (chan->iobase + 0x04),
1907 atomic_read (&chan->hw_ptr));
1908
1909 DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
1910 atomic_read (&chan->n_frags), missed);
1911}
1912
1913
1914static irqreturn_t via_interrupt(int irq, void *dev_id)
1915{
1916 struct via_info *card = dev_id;
1917 u32 status32;
1918
1919 /* to minimize interrupt sharing costs, we use the SGD status
1920 * shadow register to check the status of all inputs and
1921 * outputs with a single 32-bit bus read. If no interrupt
1922 * conditions are flagged, we exit immediately
1923 */
1924 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1925 if (!(status32 & VIA_INTR_MASK))
1926 {
1927#ifdef CONFIG_MIDI_VIA82CXXX
1928 if (card->midi_devc)
1929 uart401intr(irq, card->midi_devc);
1930#endif
1931 return IRQ_HANDLED;
1932 }
1933 DPRINTK ("intr, status32 == 0x%08X\n", status32);
1934
1935 /* synchronize interrupt handling under SMP. this spinlock
1936 * goes away completely on UP
1937 */
1938 spin_lock (&card->lock);
1939
1940 if (status32 & VIA_INTR_OUT)
1941 via_intr_channel (card, &card->ch_out);
1942 if (status32 & VIA_INTR_IN)
1943 via_intr_channel (card, &card->ch_in);
1944 if (status32 & VIA_INTR_FM)
1945 via_intr_channel (card, &card->ch_fm);
1946
1947 spin_unlock (&card->lock);
1948
1949 return IRQ_HANDLED;
1950}
1951
1952static irqreturn_t via_new_interrupt(int irq, void *dev_id)
1953{
1954 struct via_info *card = dev_id;
1955 u32 status32;
1956
1957 /* to minimize interrupt sharing costs, we use the SGD status
1958 * shadow register to check the status of all inputs and
1959 * outputs with a single 32-bit bus read. If no interrupt
1960 * conditions are flagged, we exit immediately
1961 */
1962 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1963 if (!(status32 & VIA_NEW_INTR_MASK))
1964 return IRQ_NONE;
1965 /*
1966 * goes away completely on UP
1967 */
1968 spin_lock (&card->lock);
1969
1970 via_intr_channel (card, &card->ch_out);
1971 via_intr_channel (card, &card->ch_in);
1972 via_intr_channel (card, &card->ch_fm);
1973
1974 spin_unlock (&card->lock);
1975 return IRQ_HANDLED;
1976}
1977
1978
1979/**
1980 * via_interrupt_init - Initialize interrupt handling
1981 * @card: Private info for specified board
1982 *
1983 * Obtain and reserve IRQ for using in handling audio events.
1984 * Also, disable any IRQ-generating resources, to make sure
1985 * we don't get interrupts before we want them.
1986 */
1987
1988static int via_interrupt_init (struct via_info *card)
1989{
1990 u8 tmp8;
1991
1992 DPRINTK ("ENTER\n");
1993
1994 assert (card != NULL);
1995 assert (card->pdev != NULL);
1996
1997 /* check for sane IRQ number. can this ever happen? */
1998 if (card->pdev->irq < 2) {
1999 printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
2000 card->pdev->irq);
2001 DPRINTK ("EXIT, returning -EIO\n");
2002 return -EIO;
2003 }
2004
2005 /* VIA requires this is done */
2006 pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2007
2008 if(card->legacy)
2009 {
2010 /* make sure FM irq is not routed to us */
2011 pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
2012 if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
2013 tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
2014 pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
2015 }
2016 if (request_irq (card->pdev->irq, via_interrupt, IRQF_SHARED, VIA_MODULE_NAME, card)) {
2017 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2018 card->pdev->irq);
2019 DPRINTK ("EXIT, returning -EBUSY\n");
2020 return -EBUSY;
2021 }
2022 }
2023 else
2024 {
2025 if (request_irq (card->pdev->irq, via_new_interrupt, IRQF_SHARED, VIA_MODULE_NAME, card)) {
2026 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2027 card->pdev->irq);
2028 DPRINTK ("EXIT, returning -EBUSY\n");
2029 return -EBUSY;
2030 }
2031 }
2032
2033 DPRINTK ("EXIT, returning 0\n");
2034 return 0;
2035}
2036
2037
2038/****************************************************************
2039 *
2040 * OSS DSP device
2041 *
2042 */
2043
2044static const struct file_operations via_dsp_fops = {
2045 .owner = THIS_MODULE,
2046 .open = via_dsp_open,
2047 .release = via_dsp_release,
2048 .read = via_dsp_read,
2049 .write = via_dsp_write,
2050 .poll = via_dsp_poll,
2051 .llseek = no_llseek,
2052 .ioctl = via_dsp_ioctl,
2053 .mmap = via_dsp_mmap,
2054};
2055
2056
2057static int __devinit via_dsp_init (struct via_info *card)
2058{
2059 u8 tmp8;
2060
2061 DPRINTK ("ENTER\n");
2062
2063 assert (card != NULL);
2064
2065 if(card->legacy)
2066 {
2067 /* turn off legacy features, if not already */
2068 pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
2069 if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) {
2070 tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
2071 pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
2072 }
2073 }
2074
2075 via_stop_everything (card);
2076
2077 card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
2078 if (card->dev_dsp < 0) {
2079 DPRINTK ("EXIT, returning -ENODEV\n");
2080 return -ENODEV;
2081 }
2082 DPRINTK ("EXIT, returning 0\n");
2083 return 0;
2084}
2085
2086
2087static void via_dsp_cleanup (struct via_info *card)
2088{
2089 DPRINTK ("ENTER\n");
2090
2091 assert (card != NULL);
2092 assert (card->dev_dsp >= 0);
2093
2094 via_stop_everything (card);
2095
2096 unregister_sound_dsp (card->dev_dsp);
2097
2098 DPRINTK ("EXIT\n");
2099}
2100
2101
2102static struct page * via_mm_nopage (struct vm_area_struct * vma,
2103 unsigned long address, int *type)
2104{
2105 struct via_info *card = vma->vm_private_data;
2106 struct via_channel *chan = &card->ch_out;
2107 unsigned long max_bufs;
2108 struct page *dmapage;
2109 unsigned long pgoff;
2110 int rd, wr;
2111
2112 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
2113 vma->vm_start,
2114 address - vma->vm_start,
2115 (address - vma->vm_start) >> PAGE_SHIFT,
2116 address);
2117
2118 if (address > vma->vm_end) {
2119 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2120 return NOPAGE_SIGBUS; /* Disallow mremap */
2121 }
2122 if (!card) {
2123 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2124 return NOPAGE_SIGBUS; /* Nothing allocated */
2125 }
2126
2127 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
2128 rd = card->ch_in.is_mapped;
2129 wr = card->ch_out.is_mapped;
2130
2131 max_bufs = chan->frag_number;
2132 if (rd && wr)
2133 max_bufs *= 2;
2134 if (pgoff >= max_bufs)
2135 return NOPAGE_SIGBUS;
2136
2137 /* if full-duplex (read+write) and we have two sets of bufs,
2138 * then the playback buffers come first, sez soundcard.c */
2139 if (pgoff >= chan->page_number) {
2140 pgoff -= chan->page_number;
2141 chan = &card->ch_in;
2142 } else if (!wr)
2143 chan = &card->ch_in;
2144
2145 assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
2146
2147 dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
2148 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2149 dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
2150 get_page (dmapage);
2151 if (type)
2152 *type = VM_FAULT_MINOR;
2153 return dmapage;
2154}
2155
2156
2157#ifndef VM_RESERVED
2158static int via_mm_swapout (struct page *page, struct file *filp)
2159{
2160 return 0;
2161}
2162#endif /* VM_RESERVED */
2163
2164
2165static struct vm_operations_struct via_mm_ops = {
2166 .nopage = via_mm_nopage,
2167
2168#ifndef VM_RESERVED
2169 .swapout = via_mm_swapout,
2170#endif
2171};
2172
2173
2174static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
2175{
2176 struct via_info *card;
2177 int nonblock = (file->f_flags & O_NONBLOCK);
2178 int rc = -EINVAL, rd=0, wr=0;
2179 unsigned long max_size, size, start, offset;
2180
2181 assert (file != NULL);
2182 assert (vma != NULL);
2183 card = file->private_data;
2184 assert (card != NULL);
2185
2186 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2187 vma->vm_start,
2188 vma->vm_end - vma->vm_start,
2189 vma->vm_pgoff);
2190
2191 max_size = 0;
2192 if (vma->vm_flags & VM_READ) {
2193 rd = 1;
2194 via_chan_set_buffering(card, &card->ch_in, -1);
2195 via_chan_buffer_init (card, &card->ch_in);
2196 max_size += card->ch_in.page_number << PAGE_SHIFT;
2197 }
2198 if (vma->vm_flags & VM_WRITE) {
2199 wr = 1;
2200 via_chan_set_buffering(card, &card->ch_out, -1);
2201 via_chan_buffer_init (card, &card->ch_out);
2202 max_size += card->ch_out.page_number << PAGE_SHIFT;
2203 }
2204
2205 start = vma->vm_start;
2206 offset = (vma->vm_pgoff << PAGE_SHIFT);
2207 size = vma->vm_end - vma->vm_start;
2208
2209 /* some basic size/offset sanity checks */
2210 if (size > max_size)
2211 goto out;
2212 if (offset > max_size - size)
2213 goto out;
2214
2215 rc = via_syscall_down (card, nonblock);
2216 if (rc) goto out;
2217
2218 vma->vm_ops = &via_mm_ops;
2219 vma->vm_private_data = card;
2220
2221#ifdef VM_RESERVED
2222 vma->vm_flags |= VM_RESERVED;
2223#endif
2224
2225 if (rd)
2226 card->ch_in.is_mapped = 1;
2227 if (wr)
2228 card->ch_out.is_mapped = 1;
2229
2230 mutex_unlock(&card->syscall_mutex);
2231 rc = 0;
2232
2233out:
2234 DPRINTK ("EXIT, returning %d\n", rc);
2235 return rc;
2236}
2237
2238
2239static ssize_t via_dsp_do_read (struct via_info *card,
2240 char __user *userbuf, size_t count,
2241 int nonblock)
2242{
2243 DECLARE_WAITQUEUE(wait, current);
2244 const char __user *orig_userbuf = userbuf;
2245 struct via_channel *chan = &card->ch_in;
2246 size_t size;
2247 int n, tmp;
2248 ssize_t ret = 0;
2249
2250 /* if SGD has not yet been started, start it */
2251 via_chan_maybe_start (chan);
2252
2253handle_one_block:
2254 /* just to be a nice neighbor */
2255 /* Thomas Sailer:
2256 * But also to ourselves, release semaphore if we do so */
2257 if (need_resched()) {
2258 mutex_unlock(&card->syscall_mutex);
2259 schedule ();
2260 ret = via_syscall_down (card, nonblock);
2261 if (ret)
2262 goto out;
2263 }
2264
2265 /* grab current channel software pointer. In the case of
2266 * recording, this is pointing to the next buffer that
2267 * will receive data from the audio hardware.
2268 */
2269 n = chan->sw_ptr;
2270
2271 /* n_frags represents the number of fragments waiting
2272 * to be copied to userland. sleep until at least
2273 * one buffer has been read from the audio hardware.
2274 */
2275 add_wait_queue(&chan->wait, &wait);
2276 for (;;) {
2277 __set_current_state(TASK_INTERRUPTIBLE);
2278 tmp = atomic_read (&chan->n_frags);
2279 assert (tmp >= 0);
2280 assert (tmp <= chan->frag_number);
2281 if (tmp)
2282 break;
2283 if (nonblock || !chan->is_active) {
2284 ret = -EAGAIN;
2285 break;
2286 }
2287
2288 mutex_unlock(&card->syscall_mutex);
2289
2290 DPRINTK ("Sleeping on block %d\n", n);
2291 schedule();
2292
2293 ret = via_syscall_down (card, nonblock);
2294 if (ret)
2295 break;
2296
2297 if (signal_pending (current)) {
2298 ret = -ERESTARTSYS;
2299 break;
2300 }
2301 }
2302 set_current_state(TASK_RUNNING);
2303 remove_wait_queue(&chan->wait, &wait);
2304 if (ret)
2305 goto out;
2306
2307 /* Now that we have a buffer we can read from, send
2308 * as much as sample data possible to userspace.
2309 */
2310 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2311 size_t slop_left = chan->frag_size - chan->slop_len;
2312 void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
2313 unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
2314
2315 size = (count < slop_left) ? count : slop_left;
2316 if (copy_to_user (userbuf,
2317 base + ofs + chan->slop_len,
2318 size)) {
2319 ret = -EFAULT;
2320 goto out;
2321 }
2322
2323 count -= size;
2324 chan->slop_len += size;
2325 userbuf += size;
2326 }
2327
2328 /* If we didn't copy the buffer completely to userspace,
2329 * stop now.
2330 */
2331 if (chan->slop_len < chan->frag_size)
2332 goto out;
2333
2334 /*
2335 * If we get to this point, we copied one buffer completely
2336 * to userspace, give the buffer back to the hardware.
2337 */
2338
2339 /* advance channel software pointer to point to
2340 * the next buffer from which we will copy
2341 */
2342 if (chan->sw_ptr == (chan->frag_number - 1))
2343 chan->sw_ptr = 0;
2344 else
2345 chan->sw_ptr++;
2346
2347 /* mark one less buffer waiting to be processed */
2348 assert (atomic_read (&chan->n_frags) > 0);
2349 atomic_dec (&chan->n_frags);
2350
2351 /* we are at a block boundary, there is no fragment data */
2352 chan->slop_len = 0;
2353
2354 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2355 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2356
2357 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2358 inb (card->baseaddr + 0x00),
2359 inb (card->baseaddr + 0x01),
2360 inb (card->baseaddr + 0x02),
2361 inl (card->baseaddr + 0x04),
2362 inl (card->baseaddr + 0x0C),
2363 inl (card->baseaddr + 0x80),
2364 inl (card->baseaddr + 0x84));
2365
2366 if (count > 0)
2367 goto handle_one_block;
2368
2369out:
2370 return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
2371}
2372
2373
2374static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2375{
2376 struct via_info *card;
2377 int nonblock = (file->f_flags & O_NONBLOCK);
2378 int rc;
2379
2380 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2381 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2382
2383 assert (file != NULL);
2384 card = file->private_data;
2385 assert (card != NULL);
2386
2387 rc = via_syscall_down (card, nonblock);
2388 if (rc) goto out;
2389
2390 if (card->ch_in.is_mapped) {
2391 rc = -ENXIO;
2392 goto out_up;
2393 }
2394
2395 via_chan_set_buffering(card, &card->ch_in, -1);
2396 rc = via_chan_buffer_init (card, &card->ch_in);
2397
2398 if (rc)
2399 goto out_up;
2400
2401 rc = via_dsp_do_read (card, buffer, count, nonblock);
2402
2403out_up:
2404 mutex_unlock(&card->syscall_mutex);
2405out:
2406 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2407 return rc;
2408}
2409
2410
2411static ssize_t via_dsp_do_write (struct via_info *card,
2412 const char __user *userbuf, size_t count,
2413 int nonblock)
2414{
2415 DECLARE_WAITQUEUE(wait, current);
2416 const char __user *orig_userbuf = userbuf;
2417 struct via_channel *chan = &card->ch_out;
2418 volatile struct via_sgd_table *sgtable = chan->sgtable;
2419 size_t size;
2420 int n, tmp;
2421 ssize_t ret = 0;
2422
2423handle_one_block:
2424 /* just to be a nice neighbor */
2425 /* Thomas Sailer:
2426 * But also to ourselves, release semaphore if we do so */
2427 if (need_resched()) {
2428 mutex_unlock(&card->syscall_mutex);
2429 schedule ();
2430 ret = via_syscall_down (card, nonblock);
2431 if (ret)
2432 goto out;
2433 }
2434
2435 /* grab current channel fragment pointer. In the case of
2436 * playback, this is pointing to the next fragment that
2437 * should receive data from userland.
2438 */
2439 n = chan->sw_ptr;
2440
2441 /* n_frags represents the number of fragments remaining
2442 * to be filled by userspace. Sleep until
2443 * at least one fragment is available for our use.
2444 */
2445 add_wait_queue(&chan->wait, &wait);
2446 for (;;) {
2447 __set_current_state(TASK_INTERRUPTIBLE);
2448 tmp = atomic_read (&chan->n_frags);
2449 assert (tmp >= 0);
2450 assert (tmp <= chan->frag_number);
2451 if (tmp)
2452 break;
2453 if (nonblock || !chan->is_active) {
2454 ret = -EAGAIN;
2455 break;
2456 }
2457
2458 mutex_unlock(&card->syscall_mutex);
2459
2460 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2461 schedule();
2462
2463 ret = via_syscall_down (card, nonblock);
2464 if (ret)
2465 break;
2466
2467 if (signal_pending (current)) {
2468 ret = -ERESTARTSYS;
2469 break;
2470 }
2471 }
2472 set_current_state(TASK_RUNNING);
2473 remove_wait_queue(&chan->wait, &wait);
2474 if (ret)
2475 goto out;
2476
2477 /* Now that we have at least one fragment we can write to, fill the buffer
2478 * as much as possible with data from userspace.
2479 */
2480 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2481 size_t slop_left = chan->frag_size - chan->slop_len;
2482
2483 size = (count < slop_left) ? count : slop_left;
2484 if (copy_from_user (chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr + (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size + chan->slop_len,
2485 userbuf, size)) {
2486 ret = -EFAULT;
2487 goto out;
2488 }
2489
2490 count -= size;
2491 chan->slop_len += size;
2492 userbuf += size;
2493 }
2494
2495 /* If we didn't fill up the buffer with data, stop now.
2496 * Put a 'stop' marker in the DMA table too, to tell the
2497 * audio hardware to stop if it gets here.
2498 */
2499 if (chan->slop_len < chan->frag_size) {
2500 sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
2501 goto out;
2502 }
2503
2504 /*
2505 * If we get to this point, we have filled a buffer with
2506 * audio data, flush the buffer to audio hardware.
2507 */
2508
2509 /* Record the true size for the audio hardware to notice */
2510 if (n == (chan->frag_number - 1))
2511 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
2512 else
2513 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
2514
2515 /* advance channel software pointer to point to
2516 * the next buffer we will fill with data
2517 */
2518 if (chan->sw_ptr == (chan->frag_number - 1))
2519 chan->sw_ptr = 0;
2520 else
2521 chan->sw_ptr++;
2522
2523 /* mark one less buffer as being available for userspace consumption */
2524 assert (atomic_read (&chan->n_frags) > 0);
2525 atomic_dec (&chan->n_frags);
2526
2527 /* we are at a block boundary, there is no fragment data */
2528 chan->slop_len = 0;
2529
2530 /* if SGD has not yet been started, start it */
2531 via_chan_maybe_start (chan);
2532
2533 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2534 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2535
2536 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2537 inb (card->baseaddr + 0x00),
2538 inb (card->baseaddr + 0x01),
2539 inb (card->baseaddr + 0x02),
2540 inl (card->baseaddr + 0x04),
2541 inl (card->baseaddr + 0x08),
2542 inl (card->baseaddr + 0x0C),
2543 inl (card->baseaddr + 0x80),
2544 inl (card->baseaddr + 0x84));
2545
2546 if (count > 0)
2547 goto handle_one_block;
2548
2549out:
2550 if (userbuf - orig_userbuf)
2551 return userbuf - orig_userbuf;
2552 else
2553 return ret;
2554}
2555
2556
2557static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2558{
2559 struct via_info *card;
2560 ssize_t rc;
2561 int nonblock = (file->f_flags & O_NONBLOCK);
2562
2563 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2564 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2565
2566 assert (file != NULL);
2567 card = file->private_data;
2568 assert (card != NULL);
2569
2570 rc = via_syscall_down (card, nonblock);
2571 if (rc) goto out;
2572
2573 if (card->ch_out.is_mapped) {
2574 rc = -ENXIO;
2575 goto out_up;
2576 }
2577
2578 via_chan_set_buffering(card, &card->ch_out, -1);
2579 rc = via_chan_buffer_init (card, &card->ch_out);
2580
2581 if (rc)
2582 goto out_up;
2583
2584 rc = via_dsp_do_write (card, buffer, count, nonblock);
2585
2586out_up:
2587 mutex_unlock(&card->syscall_mutex);
2588out:
2589 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2590 return rc;
2591}
2592
2593
2594static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
2595{
2596 struct via_info *card;
2597 struct via_channel *chan;
2598 unsigned int mask = 0;
2599
2600 DPRINTK ("ENTER\n");
2601
2602 assert (file != NULL);
2603 card = file->private_data;
2604 assert (card != NULL);
2605
2606 if (file->f_mode & FMODE_READ) {
2607 chan = &card->ch_in;
2608 if (sg_active (chan->iobase))
2609 poll_wait(file, &chan->wait, wait);
2610 if (atomic_read (&chan->n_frags) > 0)
2611 mask |= POLLIN | POLLRDNORM;
2612 }
2613
2614 if (file->f_mode & FMODE_WRITE) {
2615 chan = &card->ch_out;
2616 if (sg_active (chan->iobase))
2617 poll_wait(file, &chan->wait, wait);
2618 if (atomic_read (&chan->n_frags) > 0)
2619 mask |= POLLOUT | POLLWRNORM;
2620 }
2621
2622 DPRINTK ("EXIT, returning %u\n", mask);
2623 return mask;
2624}
2625
2626
2627/**
2628 * via_dsp_drain_playback - sleep until all playback samples are flushed
2629 * @card: Private info for specified board
2630 * @chan: Channel to drain
2631 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2632 *
2633 * Sleeps until all playback has been flushed to the audio
2634 * hardware.
2635 *
2636 * Locking: inside card->syscall_mutex
2637 */
2638
2639static int via_dsp_drain_playback (struct via_info *card,
2640 struct via_channel *chan, int nonblock)
2641{
2642 DECLARE_WAITQUEUE(wait, current);
2643 int ret = 0;
2644
2645 DPRINTK ("ENTER, nonblock = %d\n", nonblock);
2646
2647 if (chan->slop_len > 0)
2648 via_chan_flush_frag (chan);
2649
2650 if (atomic_read (&chan->n_frags) == chan->frag_number)
2651 goto out;
2652
2653 via_chan_maybe_start (chan);
2654
2655 add_wait_queue(&chan->wait, &wait);
2656 for (;;) {
2657 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
2658 __set_current_state(TASK_INTERRUPTIBLE);
2659 if (atomic_read (&chan->n_frags) >= chan->frag_number)
2660 break;
2661
2662 if (nonblock) {
2663 DPRINTK ("EXIT, returning -EAGAIN\n");
2664 ret = -EAGAIN;
2665 break;
2666 }
2667
2668#ifdef VIA_DEBUG
2669 {
2670 u8 r40,r41,r42,r43,r44,r48;
2671 pci_read_config_byte (card->pdev, 0x40, &r40);
2672 pci_read_config_byte (card->pdev, 0x41, &r41);
2673 pci_read_config_byte (card->pdev, 0x42, &r42);
2674 pci_read_config_byte (card->pdev, 0x43, &r43);
2675 pci_read_config_byte (card->pdev, 0x44, &r44);
2676 pci_read_config_byte (card->pdev, 0x48, &r48);
2677 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2678 r40,r41,r42,r43,r44,r48);
2679
2680 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2681 inb (card->baseaddr + 0x00),
2682 inb (card->baseaddr + 0x01),
2683 inb (card->baseaddr + 0x02),
2684 inl (card->baseaddr + 0x04),
2685 inl (card->baseaddr + 0x0C),
2686 inl (card->baseaddr + 0x80),
2687 inl (card->baseaddr + 0x84));
2688 }
2689
2690 if (!chan->is_active)
2691 printk (KERN_ERR "sleeping but not active\n");
2692#endif
2693
2694 mutex_unlock(&card->syscall_mutex);
2695
2696 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2697 schedule();
2698
2699 if ((ret = via_syscall_down (card, nonblock)))
2700 break;
2701
2702 if (signal_pending (current)) {
2703 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2704 ret = -ERESTARTSYS;
2705 break;
2706 }
2707 }
2708 set_current_state(TASK_RUNNING);
2709 remove_wait_queue(&chan->wait, &wait);
2710
2711#ifdef VIA_DEBUG
2712 {
2713 u8 r40,r41,r42,r43,r44,r48;
2714 pci_read_config_byte (card->pdev, 0x40, &r40);
2715 pci_read_config_byte (card->pdev, 0x41, &r41);
2716 pci_read_config_byte (card->pdev, 0x42, &r42);
2717 pci_read_config_byte (card->pdev, 0x43, &r43);
2718 pci_read_config_byte (card->pdev, 0x44, &r44);
2719 pci_read_config_byte (card->pdev, 0x48, &r48);
2720 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2721 r40,r41,r42,r43,r44,r48);
2722
2723 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2724 inb (card->baseaddr + 0x00),
2725 inb (card->baseaddr + 0x01),
2726 inb (card->baseaddr + 0x02),
2727 inl (card->baseaddr + 0x04),
2728 inl (card->baseaddr + 0x0C),
2729 inl (card->baseaddr + 0x80),
2730 inl (card->baseaddr + 0x84));
2731
2732 DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
2733 }
2734#endif
2735
2736out:
2737 DPRINTK ("EXIT, returning %d\n", ret);
2738 return ret;
2739}
2740
2741
2742/**
2743 * via_dsp_ioctl_space - get information about channel buffering
2744 * @card: Private info for specified board
2745 * @chan: pointer to channel-specific info
2746 * @arg: user buffer for returned information
2747 *
2748 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2749 *
2750 * Locking: inside card->syscall_mutex
2751 */
2752
2753static int via_dsp_ioctl_space (struct via_info *card,
2754 struct via_channel *chan,
2755 void __user *arg)
2756{
2757 audio_buf_info info;
2758
2759 via_chan_set_buffering(card, chan, -1);
2760
2761 info.fragstotal = chan->frag_number;
2762 info.fragsize = chan->frag_size;
2763
2764 /* number of full fragments we can read/write without blocking */
2765 info.fragments = atomic_read (&chan->n_frags);
2766
2767 if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
2768 info.fragments--;
2769
2770 /* number of bytes that can be read or written immediately
2771 * without blocking.
2772 */
2773 info.bytes = (info.fragments * chan->frag_size);
2774 if (chan->slop_len % chan->frag_size > 0)
2775 info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
2776
2777 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2778 info.fragstotal,
2779 info.fragsize,
2780 info.fragments,
2781 info.bytes);
2782
2783 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2784}
2785
2786
2787/**
2788 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2789 * @card: Private info for specified board
2790 * @chan: pointer to channel-specific info
2791 * @arg: user buffer for returned information
2792 *
2793 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2794 *
2795 * Locking: inside card->syscall_mutex
2796 */
2797
2798static int via_dsp_ioctl_ptr (struct via_info *card,
2799 struct via_channel *chan,
2800 void __user *arg)
2801{
2802 count_info info;
2803
2804 spin_lock_irq (&card->lock);
2805
2806 info.bytes = chan->bytes;
2807 info.blocks = chan->n_irqs;
2808 chan->n_irqs = 0;
2809
2810 spin_unlock_irq (&card->lock);
2811
2812 if (chan->is_active) {
2813 unsigned long extra;
2814 info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
2815 extra = chan->frag_size - via_sg_offset(chan);
2816 info.ptr += extra;
2817 info.bytes += extra;
2818 } else {
2819 info.ptr = 0;
2820 }
2821
2822 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2823 info.bytes,
2824 info.blocks,
2825 info.ptr);
2826
2827 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2828}
2829
2830
2831static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
2832{
2833 int enable, do_something;
2834
2835 if (chan->is_record)
2836 enable = (val & PCM_ENABLE_INPUT);
2837 else
2838 enable = (val & PCM_ENABLE_OUTPUT);
2839
2840 if (!chan->is_enabled && enable) {
2841 do_something = 1;
2842 } else if (chan->is_enabled && !enable) {
2843 do_something = -1;
2844 } else {
2845 do_something = 0;
2846 }
2847
2848 DPRINTK ("enable=%d, do_something=%d\n",
2849 enable, do_something);
2850
2851 if (chan->is_active && do_something)
2852 return -EINVAL;
2853
2854 if (do_something == 1) {
2855 chan->is_enabled = 1;
2856 via_chan_maybe_start (chan);
2857 DPRINTK ("Triggering input\n");
2858 }
2859
2860 else if (do_something == -1) {
2861 chan->is_enabled = 0;
2862 DPRINTK ("Setup input trigger\n");
2863 }
2864
2865 return 0;
2866}
2867
2868
2869static int via_dsp_ioctl (struct inode *inode, struct file *file,
2870 unsigned int cmd, unsigned long arg)
2871{
2872 int rc, rd=0, wr=0, val=0;
2873 struct via_info *card;
2874 struct via_channel *chan;
2875 int nonblock = (file->f_flags & O_NONBLOCK);
2876 int __user *ip = (int __user *)arg;
2877 void __user *p = (void __user *)arg;
2878
2879 assert (file != NULL);
2880 card = file->private_data;
2881 assert (card != NULL);
2882
2883 if (file->f_mode & FMODE_WRITE)
2884 wr = 1;
2885 if (file->f_mode & FMODE_READ)
2886 rd = 1;
2887
2888 rc = via_syscall_down (card, nonblock);
2889 if (rc)
2890 return rc;
2891 rc = -EINVAL;
2892
2893 switch (cmd) {
2894
2895 /* OSS API version. XXX unverified */
2896 case OSS_GETVERSION:
2897 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2898 rc = put_user (SOUND_VERSION, ip);
2899 break;
2900
2901 /* list of supported PCM data formats */
2902 case SNDCTL_DSP_GETFMTS:
2903 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2904 rc = put_user (AFMT_U8 | AFMT_S16_LE, ip);
2905 break;
2906
2907 /* query or set current channel's PCM data format */
2908 case SNDCTL_DSP_SETFMT:
2909 if (get_user(val, ip)) {
2910 rc = -EFAULT;
2911 break;
2912 }
2913 DPRINTK ("DSP_SETFMT, val==%d\n", val);
2914 if (val != AFMT_QUERY) {
2915 rc = 0;
2916
2917 if (rd)
2918 rc = via_chan_set_fmt (card, &card->ch_in, val);
2919
2920 if (rc >= 0 && wr)
2921 rc = via_chan_set_fmt (card, &card->ch_out, val);
2922
2923 if (rc < 0)
2924 break;
2925
2926 val = rc;
2927 } else {
2928 if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2929 (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2930 val = AFMT_S16_LE;
2931 else
2932 val = AFMT_U8;
2933 }
2934 DPRINTK ("SETFMT EXIT, returning %d\n", val);
2935 rc = put_user (val, ip);
2936 break;
2937
2938 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2939 case SNDCTL_DSP_CHANNELS:
2940 if (get_user(val, ip)) {
2941 rc = -EFAULT;
2942 break;
2943 }
2944 DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2945 if (val != 0) {
2946 rc = 0;
2947
2948 if (rd)
2949 rc = via_chan_set_stereo (card, &card->ch_in, val);
2950
2951 if (rc >= 0 && wr)
2952 rc = via_chan_set_stereo (card, &card->ch_out, val);
2953
2954 if (rc < 0)
2955 break;
2956
2957 val = rc;
2958 } else {
2959 if (rd)
2960 val = card->ch_in.channels;
2961 else
2962 val = card->ch_out.channels;
2963 }
2964 DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2965 rc = put_user (val, ip);
2966 break;
2967
2968 /* enable (val is not zero) or disable (val == 0) stereo */
2969 case SNDCTL_DSP_STEREO:
2970 if (get_user(val, ip)) {
2971 rc = -EFAULT;
2972 break;
2973 }
2974 DPRINTK ("DSP_STEREO, val==%d\n", val);
2975 rc = 0;
2976
2977 if (rd)
2978 rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
2979 if (rc >= 0 && wr)
2980 rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
2981
2982 if (rc < 0)
2983 break;
2984
2985 val = rc - 1;
2986
2987 DPRINTK ("STEREO EXIT, returning %d\n", val);
2988 rc = put_user(val, ip);
2989 break;
2990
2991 /* query or set sampling rate */
2992 case SNDCTL_DSP_SPEED:
2993 if (get_user(val, ip)) {
2994 rc = -EFAULT;
2995 break;
2996 }
2997 DPRINTK ("DSP_SPEED, val==%d\n", val);
2998 if (val < 0) {
2999 rc = -EINVAL;
3000 break;
3001 }
3002 if (val > 0) {
3003 rc = 0;
3004
3005 if (rd)
3006 rc = via_chan_set_speed (card, &card->ch_in, val);
3007 if (rc >= 0 && wr)
3008 rc = via_chan_set_speed (card, &card->ch_out, val);
3009
3010 if (rc < 0)
3011 break;
3012
3013 val = rc;
3014 } else {
3015 if (rd)
3016 val = card->ch_in.rate;
3017 else if (wr)
3018 val = card->ch_out.rate;
3019 else
3020 val = 0;
3021 }
3022 DPRINTK ("SPEED EXIT, returning %d\n", val);
3023 rc = put_user (val, ip);
3024 break;
3025
3026 /* wait until all buffers have been played, and then stop device */
3027 case SNDCTL_DSP_SYNC:
3028 DPRINTK ("DSP_SYNC\n");
3029 rc = 0;
3030 if (wr) {
3031 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3032 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3033 }
3034 break;
3035
3036 /* stop recording/playback immediately */
3037 case SNDCTL_DSP_RESET:
3038 DPRINTK ("DSP_RESET\n");
3039 if (rd) {
3040 via_chan_clear (card, &card->ch_in);
3041 card->ch_in.frag_number = 0;
3042 card->ch_in.frag_size = 0;
3043 atomic_set(&card->ch_in.n_frags, 0);
3044 }
3045
3046 if (wr) {
3047 via_chan_clear (card, &card->ch_out);
3048 card->ch_out.frag_number = 0;
3049 card->ch_out.frag_size = 0;
3050 atomic_set(&card->ch_out.n_frags, 0);
3051 }
3052
3053 rc = 0;
3054 break;
3055
3056 case SNDCTL_DSP_NONBLOCK:
3057 file->f_flags |= O_NONBLOCK;
3058 rc = 0;
3059 break;
3060
3061 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3062 case SNDCTL_DSP_GETCAPS:
3063 DPRINTK ("DSP_GETCAPS\n");
3064 rc = put_user(VIA_DSP_CAP, ip);
3065 break;
3066
3067 /* obtain buffer fragment size */
3068 case SNDCTL_DSP_GETBLKSIZE:
3069 DPRINTK ("DSP_GETBLKSIZE\n");
3070
3071 if (rd) {
3072 via_chan_set_buffering(card, &card->ch_in, -1);
3073 rc = put_user(card->ch_in.frag_size, ip);
3074 } else if (wr) {
3075 via_chan_set_buffering(card, &card->ch_out, -1);
3076 rc = put_user(card->ch_out.frag_size, ip);
3077 }
3078 break;
3079
3080 /* obtain information about input buffering */
3081 case SNDCTL_DSP_GETISPACE:
3082 DPRINTK ("DSP_GETISPACE\n");
3083 if (rd)
3084 rc = via_dsp_ioctl_space (card, &card->ch_in, p);
3085 break;
3086
3087 /* obtain information about output buffering */
3088 case SNDCTL_DSP_GETOSPACE:
3089 DPRINTK ("DSP_GETOSPACE\n");
3090 if (wr)
3091 rc = via_dsp_ioctl_space (card, &card->ch_out, p);
3092 break;
3093
3094 /* obtain information about input hardware pointer */
3095 case SNDCTL_DSP_GETIPTR:
3096 DPRINTK ("DSP_GETIPTR\n");
3097 if (rd)
3098 rc = via_dsp_ioctl_ptr (card, &card->ch_in, p);
3099 break;
3100
3101 /* obtain information about output hardware pointer */
3102 case SNDCTL_DSP_GETOPTR:
3103 DPRINTK ("DSP_GETOPTR\n");
3104 if (wr)
3105 rc = via_dsp_ioctl_ptr (card, &card->ch_out, p);
3106 break;
3107
3108 /* return number of bytes remaining to be played by DMA engine */
3109 case SNDCTL_DSP_GETODELAY:
3110 {
3111 DPRINTK ("DSP_GETODELAY\n");
3112
3113 chan = &card->ch_out;
3114
3115 if (!wr)
3116 break;
3117
3118 if (chan->is_active) {
3119
3120 val = chan->frag_number - atomic_read (&chan->n_frags);
3121
3122 assert(val >= 0);
3123
3124 if (val > 0) {
3125 val *= chan->frag_size;
3126 val -= chan->frag_size - via_sg_offset(chan);
3127 }
3128 val += chan->slop_len % chan->frag_size;
3129 } else
3130 val = 0;
3131
3132 assert (val <= (chan->frag_size * chan->frag_number));
3133
3134 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3135 rc = put_user (val, ip);
3136 break;
3137 }
3138
3139 /* handle the quick-start of a channel,
3140 * or the notification that a quick-start will
3141 * occur in the future
3142 */
3143 case SNDCTL_DSP_SETTRIGGER:
3144 if (get_user(val, ip)) {
3145 rc = -EFAULT;
3146 break;
3147 }
3148 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3149 rd, wr, card->ch_in.is_active, card->ch_out.is_active,
3150 card->ch_in.is_enabled, card->ch_out.is_enabled);
3151
3152 rc = 0;
3153
3154 if (rd)
3155 rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3156
3157 if (!rc && wr)
3158 rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3159
3160 break;
3161
3162 case SNDCTL_DSP_GETTRIGGER:
3163 val = 0;
3164 if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
3165 val |= PCM_ENABLE_INPUT;
3166 if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
3167 val |= PCM_ENABLE_OUTPUT;
3168 rc = put_user(val, ip);
3169 break;
3170
3171 /* Enable full duplex. Since we do this as soon as we are opened
3172 * with O_RDWR, this is mainly a no-op that always returns success.
3173 */
3174 case SNDCTL_DSP_SETDUPLEX:
3175 DPRINTK ("DSP_SETDUPLEX\n");
3176 if (!rd || !wr)
3177 break;
3178 rc = 0;
3179 break;
3180
3181 /* set fragment size. implemented as a successful no-op for now */
3182 case SNDCTL_DSP_SETFRAGMENT:
3183 if (get_user(val, ip)) {
3184 rc = -EFAULT;
3185 break;
3186 }
3187 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3188
3189 if (rd)
3190 rc = via_chan_set_buffering(card, &card->ch_in, val);
3191
3192 if (wr)
3193 rc = via_chan_set_buffering(card, &card->ch_out, val);
3194
3195 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3196 val & 0xFFFF,
3197 val & 0xFFFF,
3198 (val >> 16) & 0xFFFF,
3199 (val >> 16) & 0xFFFF);
3200
3201 rc = 0;
3202 break;
3203
3204 /* inform device of an upcoming pause in input (or output). */
3205 case SNDCTL_DSP_POST:
3206 DPRINTK ("DSP_POST\n");
3207 if (wr) {
3208 if (card->ch_out.slop_len > 0)
3209 via_chan_flush_frag (&card->ch_out);
3210 via_chan_maybe_start (&card->ch_out);
3211 }
3212
3213 rc = 0;
3214 break;
3215
3216 /* not implemented */
3217 default:
3218 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3219 cmd, p);
3220 break;
3221 }
3222
3223 mutex_unlock(&card->syscall_mutex);
3224 DPRINTK ("EXIT, returning %d\n", rc);
3225 return rc;
3226}
3227
3228
3229static int via_dsp_open (struct inode *inode, struct file *file)
3230{
3231 int minor = iminor(inode);
3232 struct via_info *card;
3233 struct pci_dev *pdev = NULL;
3234 struct via_channel *chan;
3235 struct pci_driver *drvr;
3236 int nonblock = (file->f_flags & O_NONBLOCK);
3237
3238 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
3239
3240 if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
3241 DPRINTK ("EXIT, returning -EINVAL\n");
3242 return -EINVAL;
3243 }
3244
3245 card = NULL;
3246 while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
3247 drvr = pci_dev_driver (pdev);
3248 if (drvr == &via_driver) {
3249 assert (pci_get_drvdata (pdev) != NULL);
3250
3251 card = pci_get_drvdata (pdev);
3252 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3253 card->dev_dsp, minor,
3254 (card->dev_dsp ^ minor) & ~0xf);
3255
3256 if (((card->dev_dsp ^ minor) & ~0xf) == 0)
3257 goto match;
3258 }
3259 }
3260
3261 DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3262 return -ENODEV;
3263
3264match:
3265 pci_dev_put(pdev);
3266 if (nonblock) {
3267 if (!mutex_trylock(&card->open_mutex)) {
3268 DPRINTK ("EXIT, returning -EAGAIN\n");
3269 return -EAGAIN;
3270 }
3271 } else {
3272 if (mutex_lock_interruptible(&card->open_mutex)) {
3273 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3274 return -ERESTARTSYS;
3275 }
3276 }
3277
3278 file->private_data = card;
3279 DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
3280
3281 /* handle input from analog source */
3282 if (file->f_mode & FMODE_READ) {
3283 chan = &card->ch_in;
3284
3285 via_chan_init (card, chan);
3286
3287 /* why is this forced to 16-bit stereo in all drivers? */
3288 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3289 chan->channels = 2;
3290
3291 // TO DO - use FIFO: via_capture_fifo(card, 1);
3292 via_chan_pcm_fmt (chan, 0);
3293 via_set_rate (card->ac97, chan, 44100);
3294 }
3295
3296 /* handle output to analog source */
3297 if (file->f_mode & FMODE_WRITE) {
3298 chan = &card->ch_out;
3299
3300 via_chan_init (card, chan);
3301
3302 if (file->f_mode & FMODE_READ) {
3303 /* if in duplex mode make the recording and playback channels
3304 have the same settings */
3305 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3306 chan->channels = 2;
3307 via_chan_pcm_fmt (chan, 0);
3308 via_set_rate (card->ac97, chan, 44100);
3309 } else {
3310 if ((minor & 0xf) == SND_DEV_DSP16) {
3311 chan->pcm_fmt = VIA_PCM_FMT_16BIT;
3312 via_chan_pcm_fmt (chan, 0);
3313 via_set_rate (card->ac97, chan, 44100);
3314 } else {
3315 via_chan_pcm_fmt (chan, 1);
3316 via_set_rate (card->ac97, chan, 8000);
3317 }
3318 }
3319 }
3320
3321 DPRINTK ("EXIT, returning 0\n");
3322 return nonseekable_open(inode, file);
3323}
3324
3325
3326static int via_dsp_release(struct inode *inode, struct file *file)
3327{
3328 struct via_info *card;
3329 int nonblock = (file->f_flags & O_NONBLOCK);
3330 int rc;
3331
3332 DPRINTK ("ENTER\n");
3333
3334 assert (file != NULL);
3335 card = file->private_data;
3336 assert (card != NULL);
3337
3338 rc = via_syscall_down (card, nonblock);
3339 if (rc) {
3340 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3341 return rc;
3342 }
3343
3344 if (file->f_mode & FMODE_WRITE) {
3345 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3346 if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */
3347 printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
3348
3349 via_chan_free (card, &card->ch_out);
3350 via_chan_buffer_free(card, &card->ch_out);
3351 }
3352
3353 if (file->f_mode & FMODE_READ) {
3354 via_chan_free (card, &card->ch_in);
3355 via_chan_buffer_free (card, &card->ch_in);
3356 }
3357
3358 mutex_unlock(&card->syscall_mutex);
3359 mutex_unlock(&card->open_mutex);
3360
3361 DPRINTK ("EXIT, returning 0\n");
3362 return 0;
3363}
3364
3365
3366/****************************************************************
3367 *
3368 * Chip setup and kernel registration
3369 *
3370 *
3371 */
3372
3373static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
3374{
3375#ifdef CONFIG_MIDI_VIA82CXXX
3376 u8 r42;
3377#endif
3378 int rc;
3379 struct via_info *card;
3380 static int printed_version;
3381
3382 DPRINTK ("ENTER\n");
3383
3384 if (printed_version++ == 0)
3385 printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
3386
3387 rc = pci_enable_device (pdev);
3388 if (rc)
3389 goto err_out;
3390
3391 rc = pci_request_regions (pdev, "via82cxxx_audio");
3392 if (rc)
3393 goto err_out_disable;
3394
3395 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3396 if (rc)
3397 goto err_out_res;
3398 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3399 if (rc)
3400 goto err_out_res;
3401
3402 card = kmalloc (sizeof (*card), GFP_KERNEL);
3403 if (!card) {
3404 printk (KERN_ERR PFX "out of memory, aborting\n");
3405 rc = -ENOMEM;
3406 goto err_out_res;
3407 }
3408
3409 pci_set_drvdata (pdev, card);
3410
3411 memset (card, 0, sizeof (*card));
3412 card->pdev = pdev;
3413 card->baseaddr = pci_resource_start (pdev, 0);
3414 card->card_num = via_num_cards++;
3415 spin_lock_init (&card->lock);
3416 spin_lock_init (&card->ac97_lock);
3417 mutex_init(&card->syscall_mutex);
3418 mutex_init(&card->open_mutex);
3419
3420 /* we must init these now, in case the intr handler needs them */
3421 via_chan_init_defaults (card, &card->ch_out);
3422 via_chan_init_defaults (card, &card->ch_in);
3423 via_chan_init_defaults (card, &card->ch_fm);
3424
3425 /* if BAR 2 is present, chip is Rev H or later,
3426 * which means it has a few extra features */
3427 if (pci_resource_start (pdev, 2) > 0)
3428 card->rev_h = 1;
3429
3430 /* Overkill for now, but more flexible done right */
3431
3432 card->intmask = id->driver_data;
3433 card->legacy = !card->intmask;
3434 card->sixchannel = id->driver_data;
3435
3436 if(card->sixchannel)
3437 printk(KERN_INFO PFX "Six channel audio available\n");
3438 if (pdev->irq < 1) {
3439 printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
3440 rc = -ENODEV;
3441 goto err_out_kfree;
3442 }
3443
3444 if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3445 printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3446 rc = -ENODEV;
3447 goto err_out_kfree;
3448 }
3449
3450 pci_set_master(pdev);
3451
3452 /*
3453 * init AC97 mixer and codec
3454 */
3455 rc = via_ac97_init (card);
3456 if (rc) {
3457 printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3458 goto err_out_kfree;
3459 }
3460
3461 /*
3462 * init DSP device
3463 */
3464 rc = via_dsp_init (card);
3465 if (rc) {
3466 printk (KERN_ERR PFX "DSP device init failed, aborting\n");
3467 goto err_out_have_mixer;
3468 }
3469
3470 /*
3471 * init and turn on interrupts, as the last thing we do
3472 */
3473 rc = via_interrupt_init (card);
3474 if (rc) {
3475 printk (KERN_ERR PFX "interrupt init failed, aborting\n");
3476 goto err_out_have_dsp;
3477 }
3478
3479 printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
3480 card->card_num + 1, card->baseaddr, pdev->irq);
3481
3482#ifdef CONFIG_MIDI_VIA82CXXX
3483 /* Disable by default */
3484 card->midi_info.io_base = 0;
3485
3486 if(card->legacy)
3487 {
3488 pci_read_config_byte (pdev, 0x42, &r42);
3489 /* Disable MIDI interrupt */
3490 pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
3491 if (r42 & VIA_CR42_MIDI_ENABLE)
3492 {
3493 if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
3494 card->midi_info.io_base = pci_resource_start (pdev, 2);
3495 else /* Address selected by byte 0x43 */
3496 {
3497 u8 r43;
3498 pci_read_config_byte (pdev, 0x43, &r43);
3499 card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
3500 }
3501
3502 card->midi_info.irq = -pdev->irq;
3503 if (probe_uart401(& card->midi_info, THIS_MODULE))
3504 {
3505 card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
3506 pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
3507 printk("Enabled Via MIDI\n");
3508 }
3509 }
3510 }
3511#endif
3512
3513 DPRINTK ("EXIT, returning 0\n");
3514 return 0;
3515
3516err_out_have_dsp:
3517 via_dsp_cleanup (card);
3518
3519err_out_have_mixer:
3520 via_ac97_cleanup (card);
3521
3522err_out_kfree:
3523#ifndef VIA_NDEBUG
3524 memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
3525#endif
3526 kfree (card);
3527
3528err_out_res:
3529 pci_release_regions (pdev);
3530
3531err_out_disable:
3532 pci_disable_device (pdev);
3533
3534err_out:
3535 pci_set_drvdata (pdev, NULL);
3536 DPRINTK ("EXIT - returning %d\n", rc);
3537 return rc;
3538}
3539
3540
3541static void __devexit via_remove_one (struct pci_dev *pdev)
3542{
3543 struct via_info *card;
3544
3545 DPRINTK ("ENTER\n");
3546
3547 assert (pdev != NULL);
3548 card = pci_get_drvdata (pdev);
3549 assert (card != NULL);
3550
3551#ifdef CONFIG_MIDI_VIA82CXXX
3552 if (card->midi_info.io_base)
3553 unload_uart401(&card->midi_info);
3554#endif
3555
3556 free_irq (card->pdev->irq, card);
3557 via_dsp_cleanup (card);
3558 via_ac97_cleanup (card);
3559
3560#ifndef VIA_NDEBUG
3561 memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
3562#endif
3563 kfree (card);
3564
3565 pci_set_drvdata (pdev, NULL);
3566
3567 pci_release_regions (pdev);
3568 pci_disable_device (pdev);
3569 pci_set_power_state (pdev, 3); /* ...zzzzzz */
3570
3571 DPRINTK ("EXIT\n");
3572 return;
3573}
3574
3575
3576/****************************************************************
3577 *
3578 * Driver initialization and cleanup
3579 *
3580 *
3581 */
3582
3583static int __init init_via82cxxx_audio(void)
3584{
3585 int rc;
3586
3587 DPRINTK ("ENTER\n");
3588
3589 rc = pci_register_driver (&via_driver);
3590 if (rc) {
3591 DPRINTK ("EXIT, returning %d\n", rc);
3592 return rc;
3593 }
3594
3595 DPRINTK ("EXIT, returning 0\n");
3596 return 0;
3597}
3598
3599
3600static void __exit cleanup_via82cxxx_audio(void)
3601{
3602 DPRINTK ("ENTER\n");
3603
3604 pci_unregister_driver (&via_driver);
3605
3606 DPRINTK ("EXIT\n");
3607}
3608
3609
3610module_init(init_via82cxxx_audio);
3611module_exit(cleanup_via82cxxx_audio);
3612
3613MODULE_AUTHOR("Jeff Garzik");
3614MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3615MODULE_LICENSE("GPL");
3616