2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Routines for control of ALi pci audio M5451
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public Lcodecnse as published by
14 * the Free Software Foundation; either version 2 of the Lcodecnse, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public Lcodecnse for more details.
22 * You should have received a copy of the GNU General Public Lcodecnse
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sound/driver.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/slab.h>
35 #include <linux/moduleparam.h>
36 #include <linux/dma-mapping.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/info.h>
40 #include <sound/ac97_codec.h>
41 #include <sound/mpu401.h>
42 #include <sound/initval.h>
44 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
45 MODULE_DESCRIPTION("ALI M5451");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
49 static int index = SNDRV_DEFAULT_IDX1; /* Index */
50 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
51 static int pcm_channels = 32;
54 module_param(index, int, 0444);
55 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
56 module_param(id, charp, 0444);
57 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
58 module_param(pcm_channels, int, 0444);
59 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
60 module_param(spdif, bool, 0444);
61 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
63 /* just for backward compatibility */
65 module_param(enable, bool, 0444);
69 * Debug part definitions
75 #define snd_ali_printk(format, args...) printk(format, ##args);
77 #define snd_ali_printk(format, args...)
81 * Constants definition
84 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
87 #define ALI_CHANNELS 32
89 #define ALI_PCM_IN_CHANNEL 31
90 #define ALI_SPDIF_IN_CHANNEL 19
91 #define ALI_SPDIF_OUT_CHANNEL 15
92 #define ALI_CENTER_CHANNEL 24
93 #define ALI_LEF_CHANNEL 23
94 #define ALI_SURR_LEFT_CHANNEL 26
95 #define ALI_SURR_RIGHT_CHANNEL 25
96 #define ALI_MODEM_IN_CHANNEL 21
97 #define ALI_MODEM_OUT_CHANNEL 20
99 #define SNDRV_ALI_VOICE_TYPE_PCM 01
100 #define SNDRV_ALI_VOICE_TYPE_OTH 02
102 #define ALI_5451_V02 0x02
108 #define ALI_LEGACY_DMAR0 0x00 // ADR0
109 #define ALI_LEGACY_DMAR4 0x04 // CNT0
110 #define ALI_LEGACY_DMAR11 0x0b // MOD
111 #define ALI_LEGACY_DMAR15 0x0f // MMR
112 #define ALI_MPUR0 0x20
113 #define ALI_MPUR1 0x21
114 #define ALI_MPUR2 0x22
115 #define ALI_MPUR3 0x23
117 #define ALI_AC97_WRITE 0x40
118 #define ALI_AC97_READ 0x44
120 #define ALI_SCTRL 0x48
121 #define ALI_SPDIF_OUT_ENABLE 0x20
122 #define ALI_SCTRL_LINE_IN2 (1 << 9)
123 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
124 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
125 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
126 #define ALI_SCTRL_CODEC1_READY (1 << 24)
127 #define ALI_SCTRL_CODEC2_READY (1 << 25)
128 #define ALI_AC97_GPIO 0x4c
129 #define ALI_AC97_GPIO_ENABLE 0x8000
130 #define ALI_AC97_GPIO_DATA_SHIFT 16
131 #define ALI_SPDIF_CS 0x70
132 #define ALI_SPDIF_CTRL 0x74
133 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
134 #define ALI_SPDIF_IN_CH_STATUS 0x40
135 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
136 #define ALI_START 0x80
137 #define ALI_STOP 0x84
138 #define ALI_CSPF 0x90
139 #define ALI_AINT 0x98
140 #define ALI_GC_CIR 0xa0
141 #define ENDLP_IE 0x00001000
142 #define MIDLP_IE 0x00002000
143 #define ALI_AINTEN 0xa4
144 #define ALI_VOLUME 0xa8
145 #define ALI_SBDELTA_DELTA_R 0xac
146 #define ALI_MISCINT 0xb0
147 #define ADDRESS_IRQ 0x00000020
148 #define TARGET_REACHED 0x00008000
149 #define MIXER_OVERFLOW 0x00000800
150 #define MIXER_UNDERFLOW 0x00000400
151 #define GPIO_IRQ 0x01000000
152 #define ALI_SBBL_SBCL 0xc0
153 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
154 #define ALI_STIMER 0xc8
155 #define ALI_GLOBAL_CONTROL 0xd4
156 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
157 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
158 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
159 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
160 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
162 #define ALI_CSO_ALPHA_FMS 0xe0
164 #define ALI_ESO_DELTA 0xe8
165 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
166 #define ALI_EBUF1 0xf4
167 #define ALI_EBUF2 0xf8
169 #define ALI_REG(codec, x) ((codec)->port + x)
175 struct snd_ali_voice;
177 struct snd_ali_channel_control {
186 // register addresses
192 unsigned int ac97read;
193 unsigned int ac97write;
198 struct snd_ali_voice {
207 struct snd_ali *codec;
208 struct snd_pcm_substream *substream;
209 struct snd_ali_voice *extra;
211 unsigned int running: 1;
213 int eso; /* final ESO value for channel */
214 int count; /* runtime->period_size */
219 void (*private_free)(void *private_data);
225 struct snd_ali_voice voices[ALI_CHANNELS];
227 unsigned int chcnt; /* num of opened channels */
228 unsigned int chmap; /* bitmap for opened channels */
229 unsigned int synthcount;
235 #define ALI_GLOBAL_REGS 56
236 #define ALI_CHANNEL_REGS 8
237 struct snd_ali_image {
238 unsigned long regs[ALI_GLOBAL_REGS];
239 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
247 unsigned char revision;
249 unsigned int hw_initialized: 1;
250 unsigned int spdif_support: 1;
253 struct pci_dev *pci_m1533;
254 struct pci_dev *pci_m7101;
256 struct snd_card *card;
257 struct snd_pcm *pcm[MAX_CODECS];
258 struct snd_alidev synth;
259 struct snd_ali_channel_control chregs;
262 unsigned int spdif_mask;
264 unsigned int spurious_irq_count;
265 unsigned int spurious_irq_max_delta;
267 unsigned int num_of_codecs;
269 struct snd_ac97_bus *ac97_bus;
270 struct snd_ac97 *ac97[MAX_CODECS];
271 unsigned short ac97_ext_id;
272 unsigned short ac97_ext_status;
275 spinlock_t voice_alloc;
278 struct snd_ali_image *image;
282 static struct pci_device_id snd_ali_ids[] = {
283 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
286 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
288 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
289 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
290 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short, unsigned short);
298 static void ali_read_regs(struct snd_ali *codec, int channel)
303 printk("channel %d registers map:\n", channel);
304 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
308 printk("%2.2x ", j*4);
311 for (i=0; i<=0xf8/4;i++) {
313 printk("%2.2x ", (i*4/0x10)*0x10);
314 dwVal = inl(ALI_REG(codec,i*4));
315 printk("%8.8x ", dwVal);
321 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
324 struct pci_dev *pci_dev;
327 pci_dev = pci_get_device(vendor, deviceid, NULL);
331 printk("\nM%x PCI CFG\n", deviceid);
341 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
342 printk("%8.8x ", dwVal);
346 pci_dev_put(pci_dev);
348 static void ali_read_ac97regs(struct snd_ali *codec, int secondary)
353 printk("\ncodec %d registers map:\n", secondary);
357 printk("%2.2x ",j*2);
360 for (i=0; i<64;i++) {
362 printk("%2.2x ", (i/8)*0x10);
363 wVal = snd_ali_codec_peek(codec, secondary, i*2);
364 printk("%4.4x ", wVal);
377 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
380 return (unsigned int)inl(ALI_REG(codec, port));
383 static inline void snd_ali_5451_poke( struct snd_ali *codec,
387 outl((unsigned int)val, ALI_REG(codec, port));
390 static int snd_ali_codec_ready( struct snd_ali *codec,
393 unsigned long end_time;
396 end_time = jiffies + msecs_to_jiffies(250);
398 res = snd_ali_5451_peek(codec,port);
399 if (! (res & 0x8000))
401 schedule_timeout_uninterruptible(1);
402 } while (time_after_eq(end_time, jiffies));
403 snd_ali_5451_poke(codec, port, res & ~0x8000);
404 snd_printdd("ali_codec_ready: codec is not ready.\n ");
408 static int snd_ali_stimer_ready(struct snd_ali *codec)
410 unsigned long end_time;
411 unsigned long dwChk1,dwChk2;
413 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
414 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
416 end_time = jiffies + msecs_to_jiffies(250);
418 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
419 if (dwChk2 != dwChk1)
421 schedule_timeout_uninterruptible(1);
422 } while (time_after_eq(end_time, jiffies));
423 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
427 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
431 unsigned int dwVal = 0;
432 unsigned int port = 0;
435 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
439 port = codec->chregs.regs.ac97write;
441 if (snd_ali_codec_ready(codec, port) < 0)
443 if (snd_ali_stimer_ready(codec) < 0)
446 dwVal = (unsigned int) (reg & 0xff);
447 dwVal |= 0x8000 | (val << 16);
448 if (secondary) dwVal |= 0x0080;
449 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
451 snd_ali_5451_poke(codec,port,dwVal);
456 static unsigned short snd_ali_codec_peek( struct snd_ali *codec,
460 unsigned int dwVal = 0;
461 unsigned int port = 0;
464 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
468 port = codec->chregs.regs.ac97read;
470 if (snd_ali_codec_ready(codec, port) < 0)
472 if (snd_ali_stimer_ready(codec) < 0)
475 dwVal = (unsigned int) (reg & 0xff);
476 dwVal |= 0x8000; /* bit 15*/
477 if (secondary) dwVal |= 0x0080;
479 snd_ali_5451_poke(codec, port, dwVal);
481 if (snd_ali_stimer_ready(codec) < 0)
483 if (snd_ali_codec_ready(codec, port) < 0)
486 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
489 static void snd_ali_codec_write(struct snd_ac97 *ac97,
493 struct snd_ali *codec = ac97->private_data;
495 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
496 if(reg == AC97_GPIO_STATUS) {
497 outl((val << ALI_AC97_GPIO_DATA_SHIFT)|ALI_AC97_GPIO_ENABLE,
498 ALI_REG(codec, ALI_AC97_GPIO));
501 snd_ali_codec_poke(codec, ac97->num, reg, val);
506 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
508 struct snd_ali *codec = ac97->private_data;
510 snd_ali_printk("codec_read reg=%xh.\n", reg);
511 return (snd_ali_codec_peek(codec, ac97->num, reg));
518 static int snd_ali_reset_5451(struct snd_ali *codec)
520 struct pci_dev *pci_dev = NULL;
521 unsigned short wCount, wReg;
524 if ((pci_dev = codec->pci_m1533) != NULL) {
525 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
526 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
528 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
529 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
533 pci_dev = codec->pci;
534 pci_read_config_dword(pci_dev, 0x44, &dwVal);
535 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
537 pci_read_config_dword(pci_dev, 0x44, &dwVal);
538 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
543 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
544 if((wReg & 0x000f) == 0x000f)
549 /* non-fatal if you have a non PM capable codec */
550 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
556 static int snd_ali_reset_codec(struct snd_ali *codec)
558 struct pci_dev *pci_dev = NULL;
559 unsigned char bVal = 0;
561 unsigned short wCount, wReg;
563 pci_dev = codec->pci_m1533;
565 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
566 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
568 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
569 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
572 bVal = inb(ALI_REG(codec,ALI_SCTRL));
574 outb(ALI_REG(codec,ALI_SCTRL),bVal);
576 bVal = inb(ALI_REG(codec,ALI_SCTRL));
578 outb(ALI_REG(codec,ALI_SCTRL),bVal);
583 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
584 if((wReg & 0x000f) == 0x000f)
594 * ALI 5451 Controller
597 static void snd_ali_enable_special_channel(struct snd_ali *codec, unsigned int channel)
599 unsigned long dwVal = 0;
601 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
602 dwVal |= 1 << (channel & 0x0000001f);
603 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
606 static void snd_ali_disable_special_channel(struct snd_ali *codec, unsigned int channel)
608 unsigned long dwVal = 0;
610 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
611 dwVal &= ~(1 << (channel & 0x0000001f));
612 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
615 static void snd_ali_enable_address_interrupt(struct snd_ali * codec)
619 gc = inl(ALI_REG(codec, ALI_GC_CIR));
622 outl( gc, ALI_REG(codec, ALI_GC_CIR));
625 static void snd_ali_disable_address_interrupt(struct snd_ali * codec)
629 gc = inl(ALI_REG(codec, ALI_GC_CIR));
632 outl(gc, ALI_REG(codec, ALI_GC_CIR));
636 static void snd_ali_enable_voice_irq(struct snd_ali *codec, unsigned int channel)
639 struct snd_ali_channel_control *pchregs = &(codec->chregs);
641 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
643 mask = 1 << (channel & 0x1f);
644 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
645 pchregs->data.ainten |= mask;
646 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
650 static void snd_ali_disable_voice_irq(struct snd_ali *codec, unsigned int channel)
653 struct snd_ali_channel_control *pchregs = &(codec->chregs);
655 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
657 mask = 1 << (channel & 0x1f);
658 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
659 pchregs->data.ainten &= ~mask;
660 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
663 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
665 unsigned int idx = channel & 0x1f;
667 if (codec->synth.chcnt >= ALI_CHANNELS){
668 snd_printk(KERN_ERR "ali_alloc_pcm_channel: no free channels.\n");
672 if (!(codec->synth.chmap & (1 << idx))) {
673 codec->synth.chmap |= 1 << idx;
674 codec->synth.chcnt++;
675 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
681 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
686 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
690 if (codec->spdif_support &&
691 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
692 idx = ALI_SPDIF_IN_CHANNEL;
694 idx = ALI_PCM_IN_CHANNEL;
696 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
699 snd_printk(KERN_ERR "ali_find_free_channel: record channel is busy now.\n");
705 if (codec->spdif_support &&
706 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
707 idx = ALI_SPDIF_OUT_CHANNEL;
708 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
711 snd_printk(KERN_ERR "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
715 for (idx = 0; idx < ALI_CHANNELS; idx++) {
716 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
719 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
723 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
725 unsigned int idx = channel & 0x0000001f;
727 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
729 if (channel < 0 || channel >= ALI_CHANNELS)
732 if (!(codec->synth.chmap & (1 << idx))) {
733 snd_printk(KERN_ERR "ali_free_channel_pcm: channel %d is not in use.\n",channel);
736 codec->synth.chmap &= ~(1 << idx);
737 codec->synth.chcnt--;
742 static void snd_ali_start_voice(struct snd_ali * codec, unsigned int channel)
744 unsigned int mask = 1 << (channel & 0x1f);
746 snd_ali_printk("start_voice: channel=%d\n",channel);
747 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
751 static void snd_ali_stop_voice(struct snd_ali * codec, unsigned int channel)
753 unsigned int mask = 1 << (channel & 0x1f);
755 snd_ali_printk("stop_voice: channel=%d\n",channel);
756 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
763 static void snd_ali_delay(struct snd_ali *codec,int interval)
765 unsigned long begintimer,currenttimer;
767 begintimer = inl(ALI_REG(codec, ALI_STIMER));
768 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
770 while (currenttimer < begintimer + interval) {
771 if(snd_ali_stimer_ready(codec) < 0)
773 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
777 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
781 u8 bval = 0, R1 = 0, R2 = 0;
783 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
785 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
787 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
789 snd_ali_delay(codec, 6);
790 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
795 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
800 while (count++ <= 50000) {
801 snd_ali_delay(codec, 6);
802 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
804 if (R2 != R1) R1 = R2; else break;
808 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
812 if (R2 >= 0x0b && R2 <= 0x0e) {
813 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
815 wval |= (u16)0x09 << 8 | (u16)0x05;
816 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
818 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
819 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
820 } else if (R2 == 0x12) {
821 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
823 wval |= (u16)0x0E << 8 | (u16)0x08;
824 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
826 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
827 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
831 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
836 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
839 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
841 snd_ali_detect_spdif_rate(codec);
843 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
846 if (bval == 0) dwRate = 44100;
847 if (bval == 1) dwRate = 48000;
848 if (bval == 2) dwRate = 32000;
853 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
857 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
858 dwVal |= ALI_SPDIF_IN_SUPPORT;
859 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
861 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
863 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
865 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
868 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
872 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
873 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
874 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
876 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
880 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
883 unsigned int dwRate = 0;
885 if (rate == 32000) dwRate = 0x300;
886 if (rate == 44100) dwRate = 0;
887 if (rate == 48000) dwRate = 0x200;
889 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
890 bVal &= (unsigned char)(~(1<<6));
892 bVal |= 0x80; //select right
893 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
894 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
896 bVal &= (~0x80); //select left
897 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
898 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
901 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
906 struct pci_dev *pci_dev = NULL;
908 pci_dev = codec->pci_m1533;
911 pci_read_config_byte(pci_dev, 0x61, &bVal);
913 pci_write_config_byte(pci_dev, 0x61, bVal);
914 pci_read_config_byte(pci_dev, 0x7d, &bVal);
916 pci_write_config_byte(pci_dev, 0x7d, bVal);
918 pci_read_config_byte(pci_dev, 0x7e, &bVal);
921 pci_write_config_byte(pci_dev, 0x7e, bVal);
923 bVal = inb(ALI_REG(codec, ALI_SCTRL));
924 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
926 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
927 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
930 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
931 wVal |= ALI_SPDIF_OUT_SEL_PCM;
932 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
933 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
937 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
939 unsigned short wVal = 0;
941 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
942 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
943 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
945 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
946 if (flag & ALI_SPDIF_OUT_NON_PCM)
950 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
952 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
955 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
957 unsigned short wVal = 0;
958 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
959 wVal |= ALI_SPDIF_OUT_SEL_PCM;
960 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
962 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
965 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
969 bVal = inb(ALI_REG(codec, ALI_SCTRL));
970 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
972 snd_ali_disable_spdif_chnout(codec);
975 static void snd_ali_update_ptr(struct snd_ali *codec,int channel)
977 struct snd_ali_voice *pvoice = NULL;
978 struct snd_pcm_runtime *runtime;
979 struct snd_ali_channel_control *pchregs = NULL;
980 unsigned int old, mask;
982 unsigned int temp, cspf;
985 pchregs = &(codec->chregs);
987 // check if interrupt occurred for channel
988 old = pchregs->data.aint;
989 mask = ((unsigned int) 1L) << (channel & 0x1f);
994 pvoice = &codec->synth.voices[channel];
995 runtime = pvoice->substream->runtime;
998 spin_lock(&codec->reg_lock);
1000 if (pvoice->pcm && pvoice->substream) {
1003 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1004 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1005 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1007 if (pvoice->running) {
1008 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1009 spin_unlock(&codec->reg_lock);
1010 snd_pcm_period_elapsed(pvoice->substream);
1011 spin_lock(&codec->reg_lock);
1013 snd_ali_stop_voice(codec, channel);
1014 snd_ali_disable_voice_irq(codec, channel);
1016 } else if (codec->synth.voices[channel].synth) {
1017 /* synth interrupt */
1018 } else if (codec->synth.voices[channel].midi) {
1019 /* midi interrupt */
1021 /* unknown interrupt */
1022 snd_ali_stop_voice(codec, channel);
1023 snd_ali_disable_voice_irq(codec, channel);
1025 spin_unlock(&codec->reg_lock);
1026 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1027 pchregs->data.aint = old & (~mask);
1030 static void snd_ali_interrupt(struct snd_ali * codec)
1033 unsigned int audio_int;
1034 struct snd_ali_channel_control *pchregs = NULL;
1035 pchregs = &(codec->chregs);
1037 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1038 if (audio_int & ADDRESS_IRQ) {
1039 // get interrupt status for all channels
1040 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1041 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1042 snd_ali_update_ptr(codec, channel);
1045 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1046 ALI_REG(codec,ALI_MISCINT));
1050 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
1052 struct snd_ali *codec = dev_id;
1056 snd_ali_interrupt(codec);
1061 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec, int type, int rec, int channel)
1063 struct snd_ali_voice *pvoice = NULL;
1066 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1068 spin_lock_irq(&codec->voice_alloc);
1069 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1070 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1071 snd_ali_find_free_channel(codec,rec);
1073 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
1074 spin_unlock_irq(&codec->voice_alloc);
1077 pvoice = &(codec->synth.voices[idx]);
1078 pvoice->codec = codec;
1082 spin_unlock_irq(&codec->voice_alloc);
1085 spin_unlock_irq(&codec->voice_alloc);
1090 static void snd_ali_free_voice(struct snd_ali * codec, struct snd_ali_voice *pvoice)
1092 void (*private_free)(void *);
1095 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1096 if (pvoice == NULL || !pvoice->use)
1098 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1099 spin_lock_irq(&codec->voice_alloc);
1100 private_free = pvoice->private_free;
1101 private_data = pvoice->private_data;
1102 pvoice->private_free = NULL;
1103 pvoice->private_data = NULL;
1105 snd_ali_free_channel_pcm(codec, pvoice->number);
1107 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1108 pvoice->substream = NULL;
1109 spin_unlock_irq(&codec->voice_alloc);
1111 private_free(private_data);
1115 static void snd_ali_clear_voices(struct snd_ali * codec,
1121 for (i = v_min; i <= v_max; i++) {
1122 snd_ali_stop_voice(codec, i);
1123 snd_ali_disable_voice_irq(codec, i);
1127 static void snd_ali_write_voice_regs(struct snd_ali * codec,
1128 unsigned int Channel,
1133 unsigned int ALPHA_FMS,
1140 unsigned int ctlcmds[4];
1142 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1144 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1146 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1147 ctlcmds[3] = (GVSEL << 31) |
1148 ((PAN & 0x0000007f) << 24) |
1149 ((VOL & 0x000000ff) << 16) |
1150 ((CTRL & 0x0000000f) << 12) |
1153 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1155 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1156 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1157 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1158 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1160 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1161 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1164 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1168 if (rate < 4000) rate = 4000;
1169 if (rate > 48000) rate = 48000;
1174 else if (rate == 8000)
1176 else if (rate == 48000)
1179 delta = ((48000 << 12) / rate) & 0x0000ffff;
1183 else if (rate == 8000)
1185 else if (rate == 48000)
1188 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1194 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1197 struct snd_pcm_runtime *runtime = substream->runtime;
1200 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1203 if (snd_pcm_format_width(runtime->format) == 16)
1204 CTRL |= 0x00000008; // 16-bit data
1205 if (!snd_pcm_format_unsigned(runtime->format))
1206 CTRL |= 0x00000002; // signed data
1207 if (runtime->channels > 1)
1208 CTRL |= 0x00000004; // stereo data
1216 static int snd_ali_ioctl(struct snd_pcm_substream *substream,
1217 unsigned int cmd, void *arg)
1219 return snd_pcm_lib_ioctl(substream, cmd, arg);
1222 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1226 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1227 struct snd_pcm_substream *s;
1228 unsigned int what, whati, capture_flag;
1229 struct snd_ali_voice *pvoice = NULL, *evoice = NULL;
1234 case SNDRV_PCM_TRIGGER_START:
1235 case SNDRV_PCM_TRIGGER_RESUME:
1236 do_start = 1; break;
1237 case SNDRV_PCM_TRIGGER_STOP:
1238 case SNDRV_PCM_TRIGGER_SUSPEND:
1239 do_start = 0; break;
1244 what = whati = capture_flag = 0;
1245 snd_pcm_group_for_each_entry(s, substream) {
1246 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1247 pvoice = s->runtime->private_data;
1248 evoice = pvoice->extra;
1249 what |= 1 << (pvoice->number & 0x1f);
1250 if (evoice == NULL) {
1251 whati |= 1 << (pvoice->number & 0x1f);
1253 whati |= 1 << (evoice->number & 0x1f);
1254 what |= 1 << (evoice->number & 0x1f);
1257 pvoice->running = 1;
1259 evoice->running = 1;
1261 pvoice->running = 0;
1263 evoice->running = 0;
1265 snd_pcm_trigger_done(s, substream);
1270 spin_lock(&codec->reg_lock);
1272 outl(what, ALI_REG(codec, ALI_STOP));
1274 val = inl(ALI_REG(codec, ALI_AINTEN));
1280 outl(val, ALI_REG(codec, ALI_AINTEN));
1282 outl(what, ALI_REG(codec, ALI_START));
1284 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1285 spin_unlock(&codec->reg_lock);
1290 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1291 struct snd_pcm_hw_params *hw_params)
1293 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1294 struct snd_pcm_runtime *runtime = substream->runtime;
1295 struct snd_ali_voice *pvoice = runtime->private_data;
1296 struct snd_ali_voice *evoice = pvoice->extra;
1298 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1299 if (err < 0) return err;
1301 /* voice management */
1303 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1304 if (evoice == NULL) {
1305 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0, -1);
1308 pvoice->extra = evoice;
1309 evoice->substream = substream;
1312 if (evoice != NULL) {
1313 snd_ali_free_voice(codec, evoice);
1314 pvoice->extra = evoice = NULL;
1321 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1323 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1324 struct snd_pcm_runtime *runtime = substream->runtime;
1325 struct snd_ali_voice *pvoice = runtime->private_data;
1326 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1328 snd_pcm_lib_free_pages(substream);
1329 if (evoice != NULL) {
1330 snd_ali_free_voice(codec, evoice);
1331 pvoice->extra = NULL;
1336 static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1337 struct snd_pcm_hw_params *hw_params)
1339 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1342 static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1344 return snd_pcm_lib_free_pages(substream);
1347 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1349 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1350 struct snd_pcm_runtime *runtime = substream->runtime;
1351 struct snd_ali_voice *pvoice = runtime->private_data;
1352 struct snd_ali_voice *evoice = pvoice->extra;
1363 snd_ali_printk("playback_prepare ...\n");
1365 spin_lock_irq(&codec->reg_lock);
1367 /* set Delta (rate) value */
1368 Delta = snd_ali_convert_rate(runtime->rate, 0);
1370 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1371 (pvoice->number == ALI_PCM_IN_CHANNEL))
1372 snd_ali_disable_special_channel(codec, pvoice->number);
1373 else if (codec->spdif_support &&
1374 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1375 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1376 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1380 /* set Loop Back Address */
1381 LBA = runtime->dma_addr;
1383 /* set interrupt count size */
1384 pvoice->count = runtime->period_size;
1386 /* set target ESO for channel */
1387 pvoice->eso = runtime->buffer_size;
1389 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1391 /* set ESO to capture first MIDLP interrupt */
1392 ESO = pvoice->eso -1;
1394 CTRL = snd_ali_control_mode(substream);
1400 snd_ali_printk("playback_prepare:\n ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1401 snd_ali_write_voice_regs( codec,
1413 if (evoice != NULL) {
1414 evoice->count = pvoice->count;
1415 evoice->eso = pvoice->count << 1;
1416 ESO = evoice->eso - 1;
1417 snd_ali_write_voice_regs(codec,
1426 (unsigned int)0x3ff,
1430 spin_unlock_irq(&codec->reg_lock);
1435 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1437 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1438 struct snd_pcm_runtime *runtime = substream->runtime;
1439 struct snd_ali_voice *pvoice = runtime->private_data;
1450 spin_lock_irq(&codec->reg_lock);
1452 snd_ali_printk("ali_prepare...\n");
1454 snd_ali_enable_special_channel(codec,pvoice->number);
1456 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1457 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1458 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1460 // Prepare capture intr channel
1461 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1465 spin_unlock_irq(&codec->reg_lock);
1466 if (codec->revision != ALI_5451_V02)
1469 rate = snd_ali_get_spdif_in_rate(codec);
1471 snd_printk(KERN_WARNING "ali_capture_preapre: spdif rate detect err!\n");
1474 spin_lock_irq(&codec->reg_lock);
1475 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1476 if (bValue & 0x10) {
1477 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1478 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
1482 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1485 // set target ESO for channel
1486 pvoice->eso = runtime->buffer_size;
1488 // set interrupt count size
1489 pvoice->count = runtime->period_size;
1491 // set Loop Back Address
1492 LBA = runtime->dma_addr;
1494 // set ESO to capture first MIDLP interrupt
1495 ESO = pvoice->eso - 1;
1496 CTRL = snd_ali_control_mode(substream);
1502 snd_ali_write_voice_regs( codec,
1516 spin_unlock_irq(&codec->reg_lock);
1522 static snd_pcm_uframes_t snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1524 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1525 struct snd_pcm_runtime *runtime = substream->runtime;
1526 struct snd_ali_voice *pvoice = runtime->private_data;
1529 spin_lock(&codec->reg_lock);
1530 if (!pvoice->running) {
1531 spin_unlock(&codec->reg_lock);
1534 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1535 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1536 spin_unlock(&codec->reg_lock);
1537 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1543 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1545 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1546 struct snd_pcm_runtime *runtime = substream->runtime;
1547 struct snd_ali_voice *pvoice = runtime->private_data;
1550 spin_lock(&codec->reg_lock);
1551 if (!pvoice->running) {
1552 spin_unlock_irq(&codec->reg_lock);
1555 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1556 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1557 spin_unlock(&codec->reg_lock);
1562 static struct snd_pcm_hardware snd_ali_playback =
1564 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1565 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1566 SNDRV_PCM_INFO_MMAP_VALID |
1567 SNDRV_PCM_INFO_RESUME |
1568 SNDRV_PCM_INFO_SYNC_START),
1569 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1570 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1571 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1576 .buffer_bytes_max = (256*1024),
1577 .period_bytes_min = 64,
1578 .period_bytes_max = (256*1024),
1580 .periods_max = 1024,
1585 * Capture support device description
1588 static struct snd_pcm_hardware snd_ali_capture =
1590 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1591 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1592 SNDRV_PCM_INFO_MMAP_VALID |
1593 SNDRV_PCM_INFO_RESUME |
1594 SNDRV_PCM_INFO_SYNC_START),
1595 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1596 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1597 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1602 .buffer_bytes_max = (128*1024),
1603 .period_bytes_min = 64,
1604 .period_bytes_max = (128*1024),
1606 .periods_max = 1024,
1610 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1612 struct snd_ali_voice *pvoice = runtime->private_data;
1613 struct snd_ali *codec;
1616 codec = pvoice->codec;
1617 snd_ali_free_voice(pvoice->codec, pvoice);
1621 static int snd_ali_open(struct snd_pcm_substream *substream, int rec, int channel,
1622 struct snd_pcm_hardware *phw)
1624 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1625 struct snd_pcm_runtime *runtime = substream->runtime;
1626 struct snd_ali_voice *pvoice;
1628 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, channel);
1632 pvoice->substream = substream;
1633 runtime->private_data = pvoice;
1634 runtime->private_free = snd_ali_pcm_free_substream;
1637 snd_pcm_set_sync(substream);
1638 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1642 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1644 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1647 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1649 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1652 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1657 static int snd_ali_close(struct snd_pcm_substream *substream)
1659 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1660 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1662 snd_ali_disable_special_channel(codec,pvoice->number);
1667 static struct snd_pcm_ops snd_ali_playback_ops = {
1668 .open = snd_ali_playback_open,
1669 .close = snd_ali_playback_close,
1670 .ioctl = snd_ali_ioctl,
1671 .hw_params = snd_ali_playback_hw_params,
1672 .hw_free = snd_ali_playback_hw_free,
1673 .prepare = snd_ali_playback_prepare,
1674 .trigger = snd_ali_trigger,
1675 .pointer = snd_ali_playback_pointer,
1678 static struct snd_pcm_ops snd_ali_capture_ops = {
1679 .open = snd_ali_capture_open,
1680 .close = snd_ali_close,
1681 .ioctl = snd_ali_ioctl,
1682 .hw_params = snd_ali_hw_params,
1683 .hw_free = snd_ali_hw_free,
1684 .prepare = snd_ali_prepare,
1685 .trigger = snd_ali_trigger,
1686 .pointer = snd_ali_pointer,
1693 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1694 struct snd_pcm_hw_params *hw_params)
1696 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1697 unsigned int modem_num = chip->num_of_codecs - 1;
1698 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, params_rate(hw_params));
1699 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1700 return snd_ali_hw_params(substream, hw_params);
1703 static struct snd_pcm_hardware snd_ali_modem =
1705 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1706 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1707 SNDRV_PCM_INFO_MMAP_VALID |
1708 SNDRV_PCM_INFO_RESUME |
1709 SNDRV_PCM_INFO_SYNC_START),
1710 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1711 .rates = SNDRV_PCM_RATE_KNOT|SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000,
1716 .buffer_bytes_max = (256*1024),
1717 .period_bytes_min = 64,
1718 .period_bytes_max = (256*1024),
1720 .periods_max = 1024,
1724 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec, int channel)
1726 static unsigned int rates [] = {8000,9600,12000,16000};
1727 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1728 .count = ARRAY_SIZE(rates),
1732 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1735 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1736 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1739 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1741 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1744 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1746 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1749 static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1750 .open = snd_ali_modem_playback_open,
1751 .close = snd_ali_close,
1752 .ioctl = snd_pcm_lib_ioctl,
1753 .hw_params = snd_ali_modem_hw_params,
1754 .hw_free = snd_ali_hw_free,
1755 .prepare = snd_ali_prepare,
1756 .trigger = snd_ali_trigger,
1757 .pointer = snd_ali_pointer,
1760 static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1761 .open = snd_ali_modem_capture_open,
1762 .close = snd_ali_close,
1763 .ioctl = snd_pcm_lib_ioctl,
1764 .hw_params = snd_ali_modem_hw_params,
1765 .hw_free = snd_ali_hw_free,
1766 .prepare = snd_ali_prepare,
1767 .trigger = snd_ali_trigger,
1768 .pointer = snd_ali_pointer,
1772 struct ali_pcm_description {
1774 unsigned int playback_num;
1775 unsigned int capture_num;
1776 struct snd_pcm_ops *playback_ops;
1777 struct snd_pcm_ops *capture_ops;
1778 unsigned short class;
1782 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1784 struct snd_ali *codec = pcm->private_data;
1785 codec->pcm[pcm->device] = NULL;
1789 static int __devinit snd_ali_pcm(struct snd_ali * codec, int device, struct ali_pcm_description *desc)
1791 struct snd_pcm *pcm;
1794 err = snd_pcm_new(codec->card, desc->name, device,
1795 desc->playback_num, desc->capture_num, &pcm);
1797 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
1800 pcm->private_data = codec;
1801 pcm->private_free = snd_ali_pcm_free;
1802 if (desc->playback_ops)
1803 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, desc->playback_ops);
1804 if (desc->capture_ops)
1805 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, desc->capture_ops);
1807 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1808 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1810 pcm->info_flags = 0;
1811 pcm->dev_class = desc->class;
1812 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1813 strcpy(pcm->name, desc->name);
1814 codec->pcm[0] = pcm;
1818 static struct ali_pcm_description ali_pcms[] = {
1819 { "ALI 5451", ALI_CHANNELS, 1, &snd_ali_playback_ops, &snd_ali_capture_ops },
1820 { "ALI 5451 modem", 1, 1, &snd_ali_modem_playback_ops, &snd_ali_modem_capture_ops, SNDRV_PCM_CLASS_MODEM }
1823 static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
1826 for(i = 0 ; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms) ; i++)
1827 if((err = snd_ali_pcm(codec, i, &ali_pcms[i])) < 0)
1833 #define ALI5451_SPDIF(xname, xindex, value) \
1834 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1835 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1836 .put = snd_ali5451_spdif_put, .private_value = value}
1838 static int snd_ali5451_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1840 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1842 uinfo->value.integer.min = 0;
1843 uinfo->value.integer.max = 1;
1847 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1849 struct snd_ali *codec = kcontrol->private_data;
1850 unsigned int enable;
1852 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1854 spin_lock_irq(&codec->reg_lock);
1855 switch(kcontrol->private_value) {
1857 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1860 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1863 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1868 ucontrol->value.integer.value[0] = enable;
1869 spin_unlock_irq(&codec->reg_lock);
1873 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1875 struct snd_ali *codec = kcontrol->private_data;
1876 unsigned int change = 0, enable = 0;
1878 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1880 spin_lock_irq(&codec->reg_lock);
1881 switch (kcontrol->private_value) {
1883 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1884 change = change ^ enable;
1887 codec->spdif_mask |= 0x02;
1888 snd_ali_enable_spdif_out(codec);
1890 codec->spdif_mask &= ~(0x02);
1891 codec->spdif_mask &= ~(0x04);
1892 snd_ali_disable_spdif_out(codec);
1897 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1898 change = change ^ enable;
1899 if (change && (codec->spdif_mask & 0x02)) {
1901 codec->spdif_mask |= 0x04;
1902 snd_ali_enable_spdif_chnout(codec);
1904 codec->spdif_mask &= ~(0x04);
1905 snd_ali_disable_spdif_chnout(codec);
1910 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1911 change = change ^ enable;
1914 codec->spdif_mask |= 0x01;
1915 snd_ali_enable_spdif_in(codec);
1917 codec->spdif_mask &= ~(0x01);
1918 snd_ali_disable_spdif_in(codec);
1925 spin_unlock_irq(&codec->reg_lock);
1930 static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
1931 /* spdif aplayback switch */
1932 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1933 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1934 /* spdif out to spdif channel */
1935 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1936 /* spdif in from spdif channel */
1937 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1940 static void snd_ali_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1942 struct snd_ali *codec = bus->private_data;
1943 codec->ac97_bus = NULL;
1946 static void snd_ali_mixer_free_ac97(struct snd_ac97 *ac97)
1948 struct snd_ali *codec = ac97->private_data;
1949 codec->ac97[ac97->num] = NULL;
1952 static int __devinit snd_ali_mixer(struct snd_ali * codec)
1954 struct snd_ac97_template ac97;
1957 static struct snd_ac97_bus_ops ops = {
1958 .write = snd_ali_codec_write,
1959 .read = snd_ali_codec_read,
1962 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1964 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1966 memset(&ac97, 0, sizeof(ac97));
1967 ac97.private_data = codec;
1968 ac97.private_free = snd_ali_mixer_free_ac97;
1970 for ( i = 0 ; i < codec->num_of_codecs ; i++) {
1972 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i])) < 0) {
1973 snd_printk(KERN_ERR "ali mixer %d creating error.\n", i);
1976 codec->num_of_codecs = 1;
1981 if (codec->spdif_support) {
1982 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1983 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1984 if (err < 0) return err;
1991 static int ali_suspend(struct pci_dev *pci, pm_message_t state)
1993 struct snd_card *card = pci_get_drvdata(pci);
1994 struct snd_ali *chip = card->private_data;
1995 struct snd_ali_image *im;
2002 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2003 for(i = 0 ; i < chip->num_of_codecs ; i++) {
2004 snd_pcm_suspend_all(chip->pcm[i]);
2005 snd_ac97_suspend(chip->ac97[i]);
2008 spin_lock_irq(&chip->reg_lock);
2010 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
2011 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
2012 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
2014 // disable all IRQ bits
2015 outl(0, ALI_REG(chip, ALI_MISCINT));
2017 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2018 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
2020 im->regs[i] = inl(ALI_REG(chip, i*4));
2023 for (i = 0; i < ALI_CHANNELS; i++) {
2024 outb(i, ALI_REG(chip, ALI_GC_CIR));
2025 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2026 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
2029 // stop all HW channel
2030 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
2032 spin_unlock_irq(&chip->reg_lock);
2034 pci_disable_device(pci);
2035 pci_save_state(pci);
2036 pci_set_power_state(pci, pci_choose_state(pci, state));
2040 static int ali_resume(struct pci_dev *pci)
2042 struct snd_card *card = pci_get_drvdata(pci);
2043 struct snd_ali *chip = card->private_data;
2044 struct snd_ali_image *im;
2051 pci_set_power_state(pci, PCI_D0);
2052 pci_restore_state(pci);
2053 if (pci_enable_device(pci) < 0) {
2054 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2055 "disabling device\n");
2056 snd_card_disconnect(card);
2059 pci_set_master(pci);
2061 spin_lock_irq(&chip->reg_lock);
2063 for (i = 0; i < ALI_CHANNELS; i++) {
2064 outb(i, ALI_REG(chip, ALI_GC_CIR));
2065 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2066 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2069 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2070 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
2072 outl(im->regs[i], ALI_REG(chip, i*4));
2076 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2077 // restore IRQ enable bits
2078 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2080 spin_unlock_irq(&chip->reg_lock);
2082 for(i = 0 ; i < chip->num_of_codecs ; i++)
2083 snd_ac97_resume(chip->ac97[i]);
2085 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2088 #endif /* CONFIG_PM */
2090 static int snd_ali_free(struct snd_ali * codec)
2092 if (codec->hw_initialized)
2093 snd_ali_disable_address_interrupt(codec);
2094 if (codec->irq >= 0) {
2095 synchronize_irq(codec->irq);
2096 free_irq(codec->irq, codec);
2099 pci_release_regions(codec->pci);
2100 pci_disable_device(codec->pci);
2102 kfree(codec->image);
2104 pci_dev_put(codec->pci_m1533);
2105 pci_dev_put(codec->pci_m7101);
2110 static int snd_ali_chip_init(struct snd_ali *codec)
2112 unsigned int legacy;
2114 struct pci_dev *pci_dev = NULL;
2116 snd_ali_printk("chip initializing ... \n");
2118 if (snd_ali_reset_5451(codec)) {
2119 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
2123 if (codec->revision == ALI_5451_V02) {
2124 pci_dev = codec->pci_m1533;
2125 pci_read_config_byte(pci_dev, 0x59, &temp);
2127 pci_write_config_byte(pci_dev, 0x59, temp);
2129 pci_dev = codec->pci_m7101;
2130 pci_read_config_byte(pci_dev, 0xb8, &temp);
2132 pci_write_config_byte(pci_dev, 0xB8, temp);
2135 pci_read_config_dword(codec->pci, 0x44, &legacy);
2136 legacy &= 0xff00ff00;
2137 legacy |= 0x000800aa;
2138 pci_write_config_dword(codec->pci, 0x44, legacy);
2140 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2141 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2142 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2143 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2144 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2146 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2147 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2148 if (codec->spdif_support) {
2149 snd_ali_enable_spdif_out(codec);
2150 codec->spdif_mask = 0x00000002;
2153 codec->num_of_codecs = 1;
2155 /* secondary codec - modem */
2156 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2157 codec->num_of_codecs++;
2158 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2159 (ALI_SCTRL_LINE_IN2|ALI_SCTRL_GPIO_IN2|ALI_SCTRL_LINE_OUT_EN),
2160 ALI_REG(codec, ALI_SCTRL));
2163 snd_ali_printk("chip initialize succeed.\n");
2168 /* proc for register dump */
2169 static void snd_ali_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
2171 struct snd_ali *codec = entry->private_data;
2173 for(i = 0 ; i < 256 ; i+= 4)
2174 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2177 static void __devinit snd_ali_proc_init(struct snd_ali *codec)
2179 struct snd_info_entry *entry;
2180 if(!snd_card_proc_new(codec->card, "ali5451", &entry))
2181 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2184 static int __devinit snd_ali_resources(struct snd_ali *codec)
2188 snd_ali_printk("resouces allocation ...\n");
2189 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2191 codec->port = pci_resource_start(codec->pci, 0);
2193 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2194 IRQF_SHARED, "ALI 5451", codec)) {
2195 snd_printk(KERN_ERR "Unable to request irq.\n");
2198 codec->irq = codec->pci->irq;
2199 snd_ali_printk("resouces allocated.\n");
2202 static int snd_ali_dev_free(struct snd_device *device)
2204 struct snd_ali *codec=device->device_data;
2205 snd_ali_free(codec);
2209 static int __devinit snd_ali_create(struct snd_card *card,
2210 struct pci_dev *pci,
2213 struct snd_ali ** r_ali)
2215 struct snd_ali *codec;
2217 unsigned short cmdw;
2218 static struct snd_device_ops ops = {
2219 .dev_free = snd_ali_dev_free,
2224 snd_ali_printk("creating ...\n");
2226 /* enable PCI device */
2227 if ((err = pci_enable_device(pci)) < 0)
2229 /* check, if we can restrict PCI DMA transfers to 31 bits */
2230 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2231 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
2232 snd_printk(KERN_ERR "architecture does not support 31bit PCI busmaster DMA\n");
2233 pci_disable_device(pci);
2237 if ((codec = kzalloc(sizeof(*codec), GFP_KERNEL)) == NULL) {
2238 pci_disable_device(pci);
2242 spin_lock_init(&codec->reg_lock);
2243 spin_lock_init(&codec->voice_alloc);
2248 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2249 codec->spdif_support = spdif_support;
2251 if (pcm_streams < 1)
2253 if (pcm_streams > 32)
2256 pci_set_master(pci);
2257 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2258 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2259 cmdw |= PCI_COMMAND_IO;
2260 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2262 pci_set_master(pci);
2264 if (snd_ali_resources(codec)) {
2265 snd_ali_free(codec);
2269 synchronize_irq(pci->irq);
2271 codec->synth.chmap = 0;
2272 codec->synth.chcnt = 0;
2273 codec->spdif_mask = 0;
2274 codec->synth.synthcount = 0;
2276 if (codec->revision == ALI_5451_V02)
2277 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2279 codec->chregs.regs.ac97read = ALI_AC97_READ;
2280 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2282 codec->chregs.regs.start = ALI_START;
2283 codec->chregs.regs.stop = ALI_STOP;
2284 codec->chregs.regs.aint = ALI_AINT;
2285 codec->chregs.regs.ainten = ALI_AINTEN;
2287 codec->chregs.data.start = 0x00;
2288 codec->chregs.data.stop = 0x00;
2289 codec->chregs.data.aint = 0x00;
2290 codec->chregs.data.ainten = 0x00;
2292 /* M1533: southbridge */
2293 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2294 if (! codec->pci_m1533) {
2295 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2296 snd_ali_free(codec);
2299 /* M7101: power management */
2300 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2301 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2302 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2303 snd_ali_free(codec);
2307 snd_ali_printk("snd_device_new is called.\n");
2308 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2309 snd_ali_free(codec);
2313 snd_card_set_dev(card, &pci->dev);
2315 /* initialise synth voices*/
2316 for (i = 0; i < ALI_CHANNELS; i++ ) {
2317 codec->synth.voices[i].number = i;
2320 if ((err = snd_ali_chip_init(codec)) < 0) {
2321 snd_printk(KERN_ERR "ali create: chip init error.\n");
2326 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2328 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2331 snd_ali_enable_address_interrupt(codec);
2332 codec->hw_initialized = 1;
2335 snd_ali_printk("created.\n");
2339 static int __devinit snd_ali_probe(struct pci_dev *pci,
2340 const struct pci_device_id *pci_id)
2342 struct snd_card *card;
2343 struct snd_ali *codec;
2346 snd_ali_printk("probe ...\n");
2348 card = snd_card_new(index, id, THIS_MODULE, 0);
2352 if ((err = snd_ali_create(card, pci, pcm_channels, spdif, &codec)) < 0) {
2353 snd_card_free(card);
2356 card->private_data = codec;
2358 snd_ali_printk("mixer building ...\n");
2359 if ((err = snd_ali_mixer(codec)) < 0) {
2360 snd_card_free(card);
2364 snd_ali_printk("pcm building ...\n");
2365 if ((err = snd_ali_build_pcms(codec)) < 0) {
2366 snd_card_free(card);
2370 snd_ali_proc_init(codec);
2372 strcpy(card->driver, "ALI5451");
2373 strcpy(card->shortname, "ALI 5451");
2375 sprintf(card->longname, "%s at 0x%lx, irq %li",
2376 card->shortname, codec->port, codec->irq);
2378 snd_ali_printk("register card.\n");
2379 if ((err = snd_card_register(card)) < 0) {
2380 snd_card_free(card);
2383 pci_set_drvdata(pci, card);
2387 static void __devexit snd_ali_remove(struct pci_dev *pci)
2389 snd_card_free(pci_get_drvdata(pci));
2390 pci_set_drvdata(pci, NULL);
2393 static struct pci_driver driver = {
2395 .id_table = snd_ali_ids,
2396 .probe = snd_ali_probe,
2397 .remove = __devexit_p(snd_ali_remove),
2399 .suspend = ali_suspend,
2400 .resume = ali_resume,
2404 static int __init alsa_card_ali_init(void)
2406 return pci_register_driver(&driver);
2409 static void __exit alsa_card_ali_exit(void)
2411 pci_unregister_driver(&driver);
2414 module_init(alsa_card_ali_init)
2415 module_exit(alsa_card_ali_exit)