38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/module.h>
57 static int index = -2;
59 static int ac97_clock = 48000;
77 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
78 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
81 #define VIA_REG_OFFSET_STATUS 0x00
82 #define VIA_REG_STAT_ACTIVE 0x80
83 #define VIA_REG_STAT_PAUSED 0x40
84 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08
85 #define VIA_REG_STAT_STOPPED 0x04
86 #define VIA_REG_STAT_EOL 0x02
87 #define VIA_REG_STAT_FLAG 0x01
88 #define VIA_REG_OFFSET_CONTROL 0x01
89 #define VIA_REG_CTRL_START 0x80
90 #define VIA_REG_CTRL_TERMINATE 0x40
91 #define VIA_REG_CTRL_AUTOSTART 0x20
92 #define VIA_REG_CTRL_PAUSE 0x08
93 #define VIA_REG_CTRL_INT_STOP 0x04
94 #define VIA_REG_CTRL_INT_EOL 0x02
95 #define VIA_REG_CTRL_INT_FLAG 0x01
96 #define VIA_REG_CTRL_RESET 0x01
97 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
98 #define VIA_REG_OFFSET_TYPE 0x02
99 #define VIA_REG_TYPE_AUTOSTART 0x80
100 #define VIA_REG_TYPE_16BIT 0x20
101 #define VIA_REG_TYPE_STEREO 0x10
102 #define VIA_REG_TYPE_INT_LLINE 0x00
103 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
104 #define VIA_REG_TYPE_INT_LESSONE 0x08
105 #define VIA_REG_TYPE_INT_MASK 0x0c
106 #define VIA_REG_TYPE_INT_EOL 0x02
107 #define VIA_REG_TYPE_INT_FLAG 0x01
108 #define VIA_REG_OFFSET_TABLE_PTR 0x04
109 #define VIA_REG_OFFSET_CURR_PTR 0x04
110 #define VIA_REG_OFFSET_STOP_IDX 0x08
111 #define VIA_REG_OFFSET_CURR_COUNT 0x0c
112 #define VIA_REG_OFFSET_CURR_INDEX 0x0f
114 #define DEFINE_VIA_REGSET(name,val) \
116 VIA_REG_##name##_STATUS = (val),\
117 VIA_REG_##name##_CONTROL = (val) + 0x01,\
118 VIA_REG_##name##_TYPE = (val) + 0x02,\
119 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
120 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
121 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
122 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
130 #define VIA_REG_AC97 0x80
131 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
132 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
133 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
134 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
135 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
136 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
137 #define VIA_REG_AC97_BUSY (1<<24)
138 #define VIA_REG_AC97_READ (1<<23)
139 #define VIA_REG_AC97_CMD_SHIFT 16
140 #define VIA_REG_AC97_CMD_MASK 0x7e
141 #define VIA_REG_AC97_DATA_SHIFT 0
142 #define VIA_REG_AC97_DATA_MASK 0xffff
144 #define VIA_REG_SGD_SHADOW 0x84
145 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
146 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
147 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
148 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
149 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
150 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
151 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
152 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
153 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
154 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
155 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
156 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
157 #define VIA_REG_SGD_STAT_MR_FLAG (1<<16)
158 #define VIA_REG_SGD_STAT_MW_FLAG (1<<17)
159 #define VIA_REG_SGD_STAT_MR_EOL (1<<20)
160 #define VIA_REG_SGD_STAT_MW_EOL (1<<21)
161 #define VIA_REG_SGD_STAT_MR_STOP (1<<24)
162 #define VIA_REG_SGD_STAT_MW_STOP (1<<25)
163 #define VIA_REG_SGD_STAT_MR_ACTIVE (1<<28)
164 #define VIA_REG_SGD_STAT_MW_ACTIVE (1<<29)
166 #define VIA_REG_GPI_STATUS 0x88
167 #define VIA_REG_GPI_INTR 0x8c
169 #define VIA_TBL_BIT_FLAG 0x40000000
170 #define VIA_TBL_BIT_EOL 0x80000000
173 #define VIA_ACLINK_STAT 0x40
174 #define VIA_ACLINK_C11_READY 0x20
175 #define VIA_ACLINK_C10_READY 0x10
176 #define VIA_ACLINK_C01_READY 0x04
177 #define VIA_ACLINK_LOWPOWER 0x02
178 #define VIA_ACLINK_C00_READY 0x01
179 #define VIA_ACLINK_CTRL 0x41
180 #define VIA_ACLINK_CTRL_ENABLE 0x80
181 #define VIA_ACLINK_CTRL_RESET 0x40
182 #define VIA_ACLINK_CTRL_SYNC 0x20
183 #define VIA_ACLINK_CTRL_SDO 0x10
184 #define VIA_ACLINK_CTRL_VRA 0x08
185 #define VIA_ACLINK_CTRL_PCM 0x04
186 #define VIA_ACLINK_CTRL_FM 0x02
187 #define VIA_ACLINK_CTRL_SB 0x01
188 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
189 VIA_ACLINK_CTRL_RESET|\
191 #define VIA_FUNC_ENABLE 0x42
192 #define VIA_FUNC_MIDI_PNP 0x80
193 #define VIA_FUNC_MIDI_IRQMASK 0x40
194 #define VIA_FUNC_RX2C_WRITE 0x20
195 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
196 #define VIA_FUNC_ENABLE_GAME 0x08
197 #define VIA_FUNC_ENABLE_FM 0x04
198 #define VIA_FUNC_ENABLE_MIDI 0x02
199 #define VIA_FUNC_ENABLE_SB 0x01
200 #define VIA_PNP_CONTROL 0x43
201 #define VIA_MC97_CTRL 0x44
202 #define VIA_MC97_CTRL_ENABLE 0x80
203 #define VIA_MC97_CTRL_SECONDARY 0x40
204 #define VIA_MC97_CTRL_INIT (VIA_MC97_CTRL_ENABLE|\
205 VIA_MC97_CTRL_SECONDARY)
217 #define VIA_TABLE_SIZE 255
236 #define VIA_MAX_MODEM_DEVS 2
280 unsigned int periods,
unsigned int fragsize)
303 for (i = 0; i < periods; i++) {
318 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
325 if (i == periods - 1)
343 dev->
bufsize = periods * fragsize;
352 if (dev->
table.area) {
365 static inline unsigned int snd_via82xx_codec_xread(
struct via82xx_modem *chip)
370 static inline void snd_via82xx_codec_xwrite(
struct via82xx_modem *chip,
unsigned int val)
375 static int snd_via82xx_codec_ready(
struct via82xx_modem *chip,
int secondary)
380 while (timeout-- > 0) {
386 secondary, snd_via82xx_codec_xread(chip));
390 static int snd_via82xx_codec_valid(
struct via82xx_modem *chip,
int secondary)
392 unsigned int timeout = 1000;
393 unsigned int val, val1;
397 while (timeout-- > 0) {
398 val = snd_via82xx_codec_xread(chip);
407 static void snd_via82xx_codec_wait(
struct snd_ac97 *
ac97)
411 err = snd_via82xx_codec_ready(chip, ac97->
num);
416 static void snd_via82xx_codec_write(
struct snd_ac97 *
ac97,
430 snd_via82xx_codec_xwrite(chip, xval);
431 snd_via82xx_codec_ready(chip, ac97->
num);
434 static unsigned short snd_via82xx_codec_read(
struct snd_ac97 *ac97,
unsigned short reg)
437 unsigned int xval, val = 0xffff;
447 ac97->
num, snd_via82xx_codec_xread(chip));
450 snd_via82xx_codec_xwrite(chip, xval);
452 if (snd_via82xx_codec_valid(chip, ac97->
num) >= 0) {
454 val = snd_via82xx_codec_xread(chip);
495 for (i = 0; i < chip->
num_devs; i++) {
496 struct viadev *viadev = &chip->
devs[
i];
497 unsigned char c_status =
inb(
VIADEV_REG(viadev, OFFSET_STATUS));
522 struct viadev *viadev = substream->
runtime->private_data;
523 unsigned char val = 0;
547 snd_via82xx_channel_reset(chip, viadev);
559 #define check_invalid_pos(viadev,pos) \
560 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
561 viadev->lastpos < viadev->bufsize2))
563 static inline unsigned int calc_linear_pos(
struct viadev *viadev,
unsigned int idx,
574 (
int)size, (
int)count);
579 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
584 if (count && size < count) {
586 "using last valid pointer\n");
599 "using last valid pointer\n");
616 struct viadev *viadev = substream->
runtime->private_data;
625 count =
inl(
VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
630 if (ptr <= (
unsigned int)viadev->
table.addr)
633 idx = ((ptr - (
unsigned int)viadev->
table.addr) / 8 - 1) %
635 res = calc_linear_pos(viadev, idx, count);
638 return bytes_to_frames(substream->
runtime, res);
649 struct viadev *viadev = substream->
runtime->private_data;
655 err = build_via_table(viadev, substream, chip->
pci,
657 params_period_bytes(hw_params));
674 struct viadev *viadev = substream->
runtime->private_data;
676 clean_via_table(viadev, substream, chip->
pci);
685 static void snd_via82xx_set_table_ptr(
struct via82xx_modem *chip,
struct viadev *viadev)
699 struct viadev *viadev = substream->
runtime->private_data;
701 snd_via82xx_channel_reset(chip, viadev);
703 snd_via82xx_set_table_ptr(chip, viadev);
737 static int snd_via82xx_modem_pcm_open(
struct via82xx_modem *chip,
struct viadev *viadev,
742 static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
749 runtime->
hw = snd_via82xx_hw;
752 &hw_constraints_rates)) < 0)
775 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
786 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
794 struct viadev *viadev = substream->
runtime->private_data;
802 static struct snd_pcm_ops snd_via686_playback_ops = {
803 .open = snd_via82xx_playback_open,
804 .close = snd_via82xx_pcm_close,
806 .hw_params = snd_via82xx_hw_params,
807 .hw_free = snd_via82xx_hw_free,
808 .prepare = snd_via82xx_pcm_prepare,
809 .trigger = snd_via82xx_pcm_trigger,
810 .pointer = snd_via686_pcm_pointer,
815 static struct snd_pcm_ops snd_via686_capture_ops = {
816 .open = snd_via82xx_capture_open,
817 .close = snd_via82xx_pcm_close,
819 .hw_params = snd_via82xx_hw_params,
820 .hw_free = snd_via82xx_hw_free,
821 .prepare = snd_via82xx_pcm_prepare,
822 .trigger = snd_via82xx_pcm_trigger,
823 .pointer = snd_via686_pcm_pointer,
828 static void init_viadev(
struct via82xx_modem *chip,
int idx,
unsigned int reg_offset,
831 chip->
devs[
idx].reg_offset = reg_offset;
858 init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
859 init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
863 64*1024, 128*1024)) < 0)
881 static void snd_via82xx_mixer_free_ac97(
struct snd_ac97 *ac97)
893 .write = snd_via82xx_codec_write,
894 .read = snd_via82xx_codec_read,
895 .wait = snd_via82xx_codec_wait,
900 chip->
ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
903 memset(&ac97, 0,
sizeof(ac97));
925 snd_iprintf(buffer,
"%s\n\n", chip->
card->longname);
926 for (i = 0; i < 0xa0; i += 4) {
927 snd_iprintf(buffer,
"%02x: %08x\n", i,
inl(chip->
port + i));
935 if (! snd_card_proc_new(chip->
card,
"via82xx", &entry))
936 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
946 unsigned long end_time;
951 pci_write_config_byte(chip->
pci, 0x44, pval|VIA_MC97_CTRL_INIT);
988 if (pval & VIA_ACLINK_C00_READY)
1022 #ifdef CONFIG_PM_SLEEP
1026 static int snd_via82xx_suspend(
struct device *dev)
1034 for (i = 0; i < 2; i++)
1035 snd_pcm_suspend_all(chip->
pcms[i]);
1036 for (i = 0; i < chip->
num_devs; i++)
1037 snd_via82xx_channel_reset(chip, &chip->
devs[i]);
1039 snd_ac97_suspend(chip->
ac97);
1047 static int snd_via82xx_resume(
struct device *dev)
1058 "disabling device\n");
1064 snd_via82xx_chip_init(chip);
1066 snd_ac97_resume(chip->
ac97);
1068 for (i = 0; i < chip->
num_devs; i++)
1069 snd_via82xx_channel_reset(chip, &chip->
devs[i]);
1075 static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
1076 #define SND_VIA82XX_PM_OPS &snd_via82xx_pm
1078 #define SND_VIA82XX_PM_OPS NULL
1088 for (i = 0; i < chip->
num_devs; i++)
1089 snd_via82xx_channel_reset(chip, &chip->
devs[i]);
1103 return snd_via82xx_free(chip);
1116 .dev_free = snd_via82xx_dev_free,
1139 KBUILD_MODNAME, chip)) {
1141 snd_via82xx_free(chip);
1145 if (ac97_clock >= 8000 && ac97_clock <= 48000)
1149 if ((err = snd_via82xx_chip_init(chip)) < 0) {
1150 snd_via82xx_free(chip);
1155 snd_via82xx_free(chip);
1196 if ((err = snd_via82xx_create(card, pci, chip_type, pci->
revision,
1197 ac97_clock, &chip)) < 0)
1200 if ((err = snd_via82xx_mixer_new(chip)) < 0)
1203 if ((err = snd_via686_pcm_new(chip)) < 0 )
1207 for (i = 0; i < chip->
num_devs; i++)
1208 snd_via82xx_channel_reset(chip, &chip->
devs[i]);
1213 snd_via82xx_proc_init(chip);
1219 pci_set_drvdata(pci, card);
1230 pci_set_drvdata(pci,
NULL);
1233 static struct pci_driver via82xx_modem_driver = {
1234 .name = KBUILD_MODNAME,
1235 .id_table = snd_via82xx_modem_ids,
1236 .probe = snd_via82xx_probe,