vserver 1.9.3
[linux-2.6.git] / sound / pci / ali5451 / ali5451.c
1 /*
2  *  Matt Wu <Matt_Wu@acersoftech.com.cn>
3  *  Apr 26, 2001
4  *  Routines for control of ALi pci audio M5451
5  *
6  *  BUGS:
7  *    --
8  *
9  *  TODO:
10  *    --
11  *
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.
16  *
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.
21  *
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
25  *
26  */
27
28 #include <sound/driver.h>
29 #include <asm/io.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 <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/info.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/mpu401.h>
41 #include <sound/initval.h>
42
43 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
44 MODULE_DESCRIPTION("ALI M5451");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
47
48 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
51 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
52 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
53 static int boot_devs;
54
55 module_param_array(index, int, boot_devs, 0444);
56 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
57 module_param_array(id, charp, boot_devs, 0444);
58 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
59 module_param_array(enable, bool, boot_devs, 0444);
60 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
61 module_param_array(pcm_channels, int, boot_devs, 0444);
62 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
63 module_param_array(spdif, bool, boot_devs, 0444);
64 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
65
66 /*
67  *  Debug part definitions
68  */
69
70 //#define ALI_DEBUG
71
72 #ifdef ALI_DEBUG
73 #define snd_ali_printk(format, args...) printk(format, ##args);
74 #else
75 #define snd_ali_printk(format, args...)
76 #endif
77
78 /*
79  *  Constants definition
80  */
81
82 #ifndef PCI_VENDOR_ID_ALI
83 #define PCI_VENDOR_ID_ALI       0x10b9
84 #endif
85
86 #ifndef PCI_DEVICE_ID_ALI_5451
87 #define PCI_DEVICE_ID_ALI_5451  0x5451
88 #endif
89
90 #define DEVICE_ID_ALI5451       ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
91
92
93 #define ALI_CHANNELS            32
94
95 #define ALI_PCM_IN_CHANNEL      31
96 #define ALI_SPDIF_IN_CHANNEL    19
97 #define ALI_SPDIF_OUT_CHANNEL   15
98 #define ALI_CENTER_CHANNEL      24
99 #define ALI_LEF_CHANNEL         23
100 #define ALI_SURR_LEFT_CHANNEL   26
101 #define ALI_SURR_RIGHT_CHANNEL  25
102
103 #define SNDRV_ALI_VOICE_TYPE_PCM        01
104 #define SNDRV_ALI_VOICE_TYPE_OTH        02
105
106 #define ALI_5451_V02            0x02
107
108 /*
109  *  Direct Registers
110  */
111
112 #define ALI_LEGACY_DMAR0        0x00  // ADR0
113 #define ALI_LEGACY_DMAR4        0x04  // CNT0
114 #define ALI_LEGACY_DMAR11       0x0b  // MOD 
115 #define ALI_LEGACY_DMAR15       0x0f  // MMR 
116 #define ALI_MPUR0               0x20
117 #define ALI_MPUR1               0x21
118 #define ALI_MPUR2               0x22
119 #define ALI_MPUR3               0x23
120
121 #define ALI_AC97_WRITE          0x40
122 #define ALI_AC97_READ           0x44
123
124 #define ALI_SCTRL               0x48
125 #define   ALI_SPDIF_OUT_ENABLE          0x20
126 #define ALI_AC97_GPIO           0x4c
127 #define ALI_SPDIF_CS            0x70
128 #define ALI_SPDIF_CTRL          0x74
129 #define   ALI_SPDIF_IN_FUNC_ENABLE      0x02
130 #define   ALI_SPDIF_IN_CH_STATUS        0x40
131 #define   ALI_SPDIF_OUT_CH_STATUS       0xbf
132 #define ALI_START               0x80
133 #define ALI_STOP                0x84
134 #define ALI_CSPF                0x90
135 #define ALI_AINT                0x98
136 #define ALI_GC_CIR              0xa0
137         #define ENDLP_IE                0x00001000
138         #define MIDLP_IE                0x00002000
139 #define ALI_AINTEN              0xa4
140 #define ALI_VOLUME              0xa8
141 #define ALI_SBDELTA_DELTA_R     0xac
142 #define ALI_MISCINT             0xb0
143         #define ADDRESS_IRQ             0x00000020
144         #define TARGET_REACHED          0x00008000
145         #define MIXER_OVERFLOW          0x00000800
146         #define MIXER_UNDERFLOW         0x00000400
147 #define ALI_SBBL_SBCL           0xc0
148 #define ALI_SBCTRL_SBE2R_SBDD   0xc4
149 #define ALI_STIMER              0xc8
150 #define ALI_GLOBAL_CONTROL      0xd4
151 #define   ALI_SPDIF_OUT_SEL_PCM         0x00000400 /* bit 10 */
152 #define   ALI_SPDIF_IN_SUPPORT          0x00000800 /* bit 11 */
153 #define   ALI_SPDIF_OUT_CH_ENABLE       0x00008000 /* bit 15 */
154 #define   ALI_SPDIF_IN_CH_ENABLE        0x00080000 /* bit 19 */
155 #define   ALI_PCM_IN_ENABLE             0x80000000 /* bit 31 */
156
157 #define ALI_CSO_ALPHA_FMS       0xe0
158 #define ALI_LBA                 0xe4
159 #define ALI_ESO_DELTA           0xe8
160 #define ALI_GVSEL_PAN_VOC_CTRL_EC       0xf0
161 #define ALI_EBUF1               0xf4
162 #define ALI_EBUF2               0xf8
163
164 #define ALI_REG(codec, x) ((codec)->port + x)
165
166 typedef struct snd_stru_ali ali_t;
167 typedef struct snd_ali_stru_voice snd_ali_voice_t;
168
169 typedef struct snd_ali_channel_control {
170         // register data
171         struct REGDATA {
172                 unsigned int start;
173                 unsigned int stop;
174                 unsigned int aint;
175                 unsigned int ainten;
176         } data;
177                 
178         // register addresses
179         struct REGS {
180                 unsigned int start;
181                 unsigned int stop;
182                 unsigned int aint;
183                 unsigned int ainten;
184                 unsigned int ac97read;
185                 unsigned int ac97write;
186         } regs;
187
188 } snd_ali_channel_control_t;
189
190 struct snd_ali_stru_voice {
191         unsigned int number;
192         int use: 1,
193             pcm: 1,
194             midi: 1,
195             mode: 1,
196             synth: 1;
197
198         /* PCM data */
199         ali_t *codec;
200         snd_pcm_substream_t *substream;
201         snd_ali_voice_t *extra;
202         
203         int running: 1;
204
205         int eso;                /* final ESO value for channel */
206         int count;              /* runtime->period_size */
207
208         /* --- */
209
210         void *private_data;
211         void (*private_free)(void *private_data);
212 };
213
214
215 typedef struct snd_stru_alidev {
216
217         snd_ali_voice_t voices[ALI_CHANNELS];   
218
219         unsigned int    chcnt;                  /* num of opened channels */
220         unsigned int    chmap;                  /* bitmap for opened channels */
221         unsigned int synthcount;
222
223 } alidev_t;
224
225
226 #ifdef CONFIG_PM
227 #define ALI_GLOBAL_REGS         56
228 #define ALI_CHANNEL_REGS        8
229 typedef struct snd_ali_image {
230         unsigned long regs[ALI_GLOBAL_REGS];
231         unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
232 } ali_image_t;
233 #endif
234
235
236 struct snd_stru_ali {
237         unsigned long   irq;
238         unsigned long   port;
239         unsigned char   revision;
240
241         unsigned int hw_initialized: 1;
242         unsigned int spdif_support: 1;
243
244         struct pci_dev  *pci;
245         struct pci_dev  *pci_m1533;
246         struct pci_dev  *pci_m7101;
247
248         snd_card_t      *card;
249         snd_pcm_t       *pcm;
250         alidev_t        synth;
251         snd_ali_channel_control_t chregs;
252
253         /* S/PDIF Mask */
254         unsigned int    spdif_mask;
255
256         unsigned int spurious_irq_count;
257         unsigned int spurious_irq_max_delta;
258
259         ac97_bus_t *ac97_bus;
260         ac97_t *ac97;
261         unsigned short  ac97_ext_id;
262         unsigned short  ac97_ext_status;
263
264         spinlock_t      reg_lock;
265         spinlock_t      voice_alloc;
266
267 #ifdef CONFIG_PM
268         ali_image_t *image;
269 #endif
270 };
271
272 static struct pci_device_id snd_ali_ids[] = {
273         {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
274         {0, }
275 };
276 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
277
278 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
279 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
280 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
281
282 /*
283  *  Debug Part
284  */
285
286 #ifdef ALI_DEBUG
287
288 static void ali_read_regs(ali_t *codec, int channel)
289 {
290         int i,j;
291         unsigned int dwVal;
292
293         printk("channel %d registers map:\n", channel);
294         outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
295
296         printk("    ");
297         for(j=0;j<8;j++)
298                 printk("%2.2x       ", j*4);
299         printk("\n");
300
301         for (i=0; i<=0xf8/4;i++) {
302                 if(i%8 == 0)
303                         printk("%2.2x  ", (i*4/0x10)*0x10);
304                 dwVal = inl(ALI_REG(codec,i*4));
305                 printk("%8.8x ", dwVal);
306                 if ((i+1)%8 == 0)
307                         printk("\n");
308         }
309         printk("\n");
310 }
311 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
312 {
313         unsigned int dwVal;
314         struct pci_dev *pci_dev = NULL;
315         int i,j;
316
317
318         pci_dev = pci_find_device(vendor, deviceid, pci_dev);
319         if (pci_dev == NULL)
320                 return ;
321
322         printk("\nM%x PCI CFG\n", deviceid);
323         printk("    ");
324         for(j=0;j<8;j++)
325                 printk("%d        ",j);
326         printk("\n");
327
328         for(i=0;i<8;i++) {
329                 printk("%d   ",i);
330                 for(j=0;j<8;j++)
331                 {
332                         pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
333                         printk("%8.8x ", dwVal);
334                 }
335                 printk("\n");
336         }
337  }
338 static void ali_read_ac97regs(ali_t *codec, int secondary)
339 {
340         unsigned short i,j;
341         unsigned short wVal;
342
343         printk("\ncodec %d registers map:\n", secondary);
344
345         printk("    ");
346         for(j=0;j<8;j++)
347                 printk("%2.2x   ",j*2);
348         printk("\n");
349
350         for (i=0; i<64;i++) {
351                 if(i%8 == 0)
352                         printk("%2.2x  ", (i/8)*0x10);
353                 wVal = snd_ali_codec_peek(codec, secondary, i*2);
354                 printk("%4.4x ", wVal);
355                 if ((i+1)%8 == 0)
356                         printk("\n");
357         }
358         printk("\n");
359 }
360
361 #endif
362
363 /*
364  *  AC97 ACCESS
365  */
366
367 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
368                                                 unsigned int port )
369 {
370         return (unsigned int)inl(ALI_REG(codec, port)); 
371 }
372
373 static inline void snd_ali_5451_poke(   ali_t *codec,
374                                         unsigned int port,
375                                         unsigned int val )
376 {
377         outl((unsigned int)val, ALI_REG(codec, port));
378 }
379
380 static int snd_ali_codec_ready( ali_t *codec,
381                                 unsigned int port,
382                                 int sched )
383 {
384         unsigned long end_time;
385         unsigned int res;
386         
387         end_time = jiffies + 10 * (HZ >> 2);
388         do {
389                 res = snd_ali_5451_peek(codec,port);
390                 if (! (res & 0x8000))
391                         return 0;
392                 if (sched) {
393                         set_current_state(TASK_UNINTERRUPTIBLE);
394                         schedule_timeout(1);
395                 }
396         } while (time_after_eq(end_time, jiffies));
397         snd_ali_5451_poke(codec, port, res & ~0x8000);
398         snd_printdd("ali_codec_ready: codec is not ready.\n ");
399         return -EIO;
400 }
401
402 static int snd_ali_stimer_ready(ali_t *codec, int sched)
403 {
404         unsigned long end_time;
405         unsigned long dwChk1,dwChk2;
406         
407         dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
408         dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
409
410         end_time = jiffies + 10 * (HZ >> 2);
411         do {
412                 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
413                 if (dwChk2 != dwChk1)
414                         return 0;
415                 if (sched) {
416                         set_current_state(TASK_UNINTERRUPTIBLE);
417                         schedule_timeout(1);
418                 }
419         } while (time_after_eq(end_time, jiffies));
420         snd_printk("ali_stimer_read: stimer is not ready.\n");
421         return -EIO;
422 }
423
424 static void snd_ali_codec_poke(ali_t *codec,int secondary,
425                                      unsigned short reg,
426                                      unsigned short val)
427 {
428         unsigned int dwVal = 0;
429         unsigned int port = 0;
430
431         if (reg >= 0x80) {
432                 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
433                 return;
434         }
435
436         port = codec->chregs.regs.ac97write;
437
438         if (snd_ali_codec_ready(codec, port, 0) < 0)
439                 return;
440         if (snd_ali_stimer_ready(codec, 0) < 0)
441                 return;
442
443         dwVal  = (unsigned int) (reg & 0xff);
444         dwVal |= 0x8000 | (val << 16);
445         if (secondary) dwVal |= 0x0080;
446         if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
447
448         snd_ali_5451_poke(codec,port,dwVal);
449
450         return ;
451 }
452
453 static unsigned short snd_ali_codec_peek( ali_t *codec,
454                                           int secondary,
455                                           unsigned short reg)
456 {
457         unsigned int dwVal = 0;
458         unsigned int port = 0;
459
460         if (reg >= 0x80) {
461                 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
462                 return ~0;
463         }
464
465         port = codec->chregs.regs.ac97read;
466
467         if (snd_ali_codec_ready(codec, port, 0) < 0)
468                 return ~0;
469         if (snd_ali_stimer_ready(codec, 0) < 0)
470                 return ~0;
471
472         dwVal  = (unsigned int) (reg & 0xff);
473         dwVal |= 0x8000;                                /* bit 15*/
474         if (secondary) dwVal |= 0x0080;
475
476         snd_ali_5451_poke(codec, port, dwVal);
477
478         if (snd_ali_stimer_ready(codec, 0) < 0)
479                 return ~0;
480         if (snd_ali_codec_ready(codec, port, 0) < 0)
481                 return ~0;
482         
483         return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
484 }
485
486 static void snd_ali_codec_write(ac97_t *ac97,
487                                 unsigned short reg,
488                                 unsigned short val )
489 {
490         ali_t *codec = ac97->private_data;
491
492         snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
493         snd_ali_codec_poke(codec, 0, reg, val);
494         return ;
495 }
496
497
498 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
499 {
500         ali_t *codec = ac97->private_data;
501
502         snd_ali_printk("codec_read reg=%xh.\n", reg);
503         return (snd_ali_codec_peek(codec, 0, reg));
504 }
505
506 /*
507  *      AC97 Reset
508  */
509
510 static int snd_ali_reset_5451(ali_t *codec)
511 {
512         struct pci_dev *pci_dev = NULL;
513         unsigned short wCount, wReg;
514         unsigned int   dwVal;
515         
516         if ((pci_dev = codec->pci_m1533) != NULL) {
517                 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
518                 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
519                 udelay(5000);
520                 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
521                 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
522                 udelay(5000);
523         }
524         
525         pci_dev = codec->pci;
526         pci_read_config_dword(pci_dev, 0x44, &dwVal);
527         pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
528         udelay(500);
529         pci_read_config_dword(pci_dev, 0x44, &dwVal);
530         pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
531         udelay(5000);
532         
533         wCount = 200;
534         while(wCount--) {
535                 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
536                 if((wReg & 0x000f) == 0x000f)
537                         return 0;
538                 udelay(5000);
539         }
540
541         /* non-fatal if you have a non PM capable codec */
542         /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
543         return 0;
544 }
545
546 #ifdef CODEC_RESET
547
548 static int snd_ali_reset_codec(ali_t *codec)
549 {
550         struct pci_dev *pci_dev = NULL;
551         unsigned char bVal = 0;
552         unsigned int   dwVal;
553         unsigned short wCount, wReg;
554
555         pci_dev = codec->pci_m1533;
556         
557         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
558         pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
559         udelay(5000);
560         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
561         pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
562         udelay(5000);
563
564         bVal = inb(ALI_REG(codec,ALI_SCTRL));
565         bVal |= 0x02;
566         outb(ALI_REG(codec,ALI_SCTRL),bVal);
567         udelay(5000);
568         bVal = inb(ALI_REG(codec,ALI_SCTRL));
569         bVal &= 0xfd;
570         outb(ALI_REG(codec,ALI_SCTRL),bVal);
571         udelay(15000);
572
573         wCount = 200;
574         while(wCount--) {
575                 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
576                 if((wReg & 0x000f) == 0x000f)
577                         return 0;
578                 udelay(5000);
579         }
580         return -1;
581 }
582
583 #endif
584
585 /*
586  *  ALI 5451 Controller
587  */
588
589 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
590 {
591         unsigned long dwVal = 0;
592
593         dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
594         dwVal |= 1 << (channel & 0x0000001f);
595         outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
596 }
597
598 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
599 {
600         unsigned long dwVal = 0;
601
602         dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
603         dwVal &= ~(1 << (channel & 0x0000001f));
604         outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
605 }
606
607 static void snd_ali_enable_address_interrupt(ali_t * codec)
608 {
609         unsigned int gc;
610
611         gc  = inl(ALI_REG(codec, ALI_GC_CIR));
612         gc |= ENDLP_IE;
613         gc |= MIDLP_IE;
614         outl( gc, ALI_REG(codec, ALI_GC_CIR));
615 }
616
617 static void snd_ali_disable_address_interrupt(ali_t * codec)
618 {
619         unsigned int gc;
620
621         gc  = inl(ALI_REG(codec, ALI_GC_CIR));
622         gc &= ~ENDLP_IE;
623         gc &= ~MIDLP_IE;
624         outl(gc, ALI_REG(codec, ALI_GC_CIR));
625 }
626
627 #if 0 // not used
628 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
629 {
630         unsigned int mask;
631         snd_ali_channel_control_t *pchregs = &(codec->chregs);
632
633         snd_ali_printk("enable_voice_irq channel=%d\n",channel);
634         
635         mask = 1 << (channel & 0x1f);
636         pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
637         pchregs->data.ainten |= mask;
638         outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
639 }
640 #endif
641
642 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
643 {
644         unsigned int mask;
645         snd_ali_channel_control_t *pchregs = &(codec->chregs);
646
647         snd_ali_printk("disable_voice_irq channel=%d\n",channel);
648
649         mask = 1 << (channel & 0x1f);
650         pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
651         pchregs->data.ainten &= ~mask;
652         outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
653 }
654
655 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
656 {
657         unsigned int idx =  channel & 0x1f;
658
659         if (codec->synth.chcnt >= ALI_CHANNELS){
660                 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
661                 return -1;
662         }
663
664         if (!(codec->synth.chmap & (1 << idx))) {
665                 codec->synth.chmap |= 1 << idx;
666                 codec->synth.chcnt++;
667                 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
668                 return idx;
669         }
670         return -1;
671 }
672
673 static int snd_ali_find_free_channel(ali_t * codec, int rec)
674 {
675         int idx;
676         int result = -1;
677
678         snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
679
680         // recording
681         if (rec) {
682                 if (codec->spdif_support &&
683                     (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
684                         idx = ALI_SPDIF_IN_CHANNEL;
685                 else
686                         idx = ALI_PCM_IN_CHANNEL;
687
688                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
689                         return result;
690                 } else {
691                         snd_printk("ali_find_free_channel: record channel is busy now.\n");
692                         return -1;
693                 }
694         }
695
696         //playback...
697         if (codec->spdif_support &&
698             (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
699                 idx = ALI_SPDIF_OUT_CHANNEL;
700                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
701                         return result;
702                 } else {
703                         snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
704                 }
705         }
706
707         for (idx = 0; idx < ALI_CHANNELS; idx++) {
708                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
709                         return result;
710         }
711         snd_printk("ali_find_free_channel: no free channels.\n");
712         return -1;
713 }
714
715 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
716 {
717         unsigned int idx = channel & 0x0000001f;
718
719         snd_ali_printk("free_channel_pcm channel=%d\n",channel);
720
721         if (channel < 0 || channel >= ALI_CHANNELS)
722                 return;
723
724         if (!(codec->synth.chmap & (1 << idx))) {
725                 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
726                 return;
727         } else {
728                 codec->synth.chmap &= ~(1 << idx);
729                 codec->synth.chcnt--;
730         }
731 }
732
733 #if 0 // not used
734 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
735 {
736         unsigned int mask = 1 << (channel & 0x1f);
737         
738         snd_ali_printk("start_voice: channel=%d\n",channel);
739         outl(mask, ALI_REG(codec,codec->chregs.regs.start));
740 }
741 #endif
742
743 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
744 {
745         unsigned int mask = 1 << (channel & 0x1f);
746
747         snd_ali_printk("stop_voice: channel=%d\n",channel);
748         outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
749 }
750
751 /*
752  *    S/PDIF Part
753  */
754
755 static void snd_ali_delay(ali_t *codec,int interval)
756 {
757         unsigned long  begintimer,currenttimer;
758
759         begintimer   = inl(ALI_REG(codec, ALI_STIMER));
760         currenttimer = inl(ALI_REG(codec, ALI_STIMER));
761
762         while (currenttimer < begintimer + interval) {
763                 if(snd_ali_stimer_ready(codec, 1) < 0)
764                         break;
765                 currenttimer = inl(ALI_REG(codec,  ALI_STIMER));
766         }
767 }
768
769 static void snd_ali_detect_spdif_rate(ali_t *codec)
770 {
771         u16 wval  = 0;
772         u16 count = 0;
773         u8  bval = 0, R1 = 0, R2 = 0;
774
775         bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
776         bval |= 0x1F;
777         outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
778
779         while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
780                 count ++;
781                 snd_ali_delay(codec, 6);
782                 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
783                 R1 = bval & 0x1F;
784         }
785
786         if (count > 50000) {
787                 snd_printk("ali_detect_spdif_rate: timeout!\n");
788                 return;
789         }
790
791         count = 0;
792         while (count++ <= 50000) {
793                 snd_ali_delay(codec, 6);
794                 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
795                 R2 = bval & 0x1F;
796                 if (R2 != R1) R1 = R2; else break;
797         }
798
799         if (count > 50000) {
800                 snd_printk("ali_detect_spdif_rate: timeout!\n");
801                 return;
802         }
803
804         if (R2 >= 0x0b && R2 <= 0x0e) {
805                 wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
806                 wval &= 0xE0F0;
807                 wval |= (u16)0x09 << 8 | (u16)0x05;
808                 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
809
810                 bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
811                 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
812         } else if (R2 == 0x12) {
813                 wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
814                 wval &= 0xE0F0;
815                 wval |= (u16)0x0E << 8 | (u16)0x08;
816                 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
817
818                 bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
819                 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
820         }
821 }
822
823 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
824 {
825         u32     dwRate = 0;
826         u8      bval = 0;
827
828         bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
829         bval &= 0x7F;
830         bval |= 0x40;
831         outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
832
833         snd_ali_detect_spdif_rate(codec);
834
835         bval  = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
836         bval &= 0x0F;
837
838         if (bval == 0) dwRate = 44100;
839         if (bval == 1) dwRate = 48000;
840         if (bval == 2) dwRate = 32000;
841
842         return dwRate;
843 }
844
845 static void snd_ali_enable_spdif_in(ali_t *codec)
846 {       
847         unsigned int dwVal;
848
849         dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
850         dwVal |= ALI_SPDIF_IN_SUPPORT;
851         outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
852
853         dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
854         dwVal |= 0x02;
855         outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
856
857         snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
858 }
859
860 static void snd_ali_disable_spdif_in(ali_t *codec)
861 {
862         unsigned int dwVal;
863         
864         dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
865         dwVal &= ~ALI_SPDIF_IN_SUPPORT;
866         outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
867         
868         snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);   
869 }
870
871
872 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
873 {
874         unsigned char  bVal;
875         unsigned int  dwRate = 0;
876         
877         if (rate == 32000) dwRate = 0x300;
878         if (rate == 44100) dwRate = 0;
879         if (rate == 48000) dwRate = 0x200;
880         
881         bVal  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
882         bVal &= (unsigned char)(~(1<<6));
883         
884         bVal |= 0x80;           //select right
885         outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
886         outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
887         
888         bVal &= (~0x80);        //select left
889         outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
890         outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
891 }
892
893 static void snd_ali_enable_spdif_out(ali_t *codec)
894 {
895         unsigned short wVal;
896         unsigned char bVal;
897
898         struct pci_dev *pci_dev = NULL;
899
900         pci_dev = codec->pci_m1533;
901         if (pci_dev == NULL)
902                 return;
903         pci_read_config_byte(pci_dev, 0x61, &bVal);
904         bVal |= 0x40;
905         pci_write_config_byte(pci_dev, 0x61, bVal);
906         pci_read_config_byte(pci_dev, 0x7d, &bVal);
907         bVal |= 0x01;
908         pci_write_config_byte(pci_dev, 0x7d, bVal);
909
910         pci_read_config_byte(pci_dev, 0x7e, &bVal);
911         bVal &= (~0x20);
912         bVal |= 0x10;
913         pci_write_config_byte(pci_dev, 0x7e, bVal);
914
915         bVal = inb(ALI_REG(codec, ALI_SCTRL));
916         outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
917
918         bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
919         outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
920    
921         {
922                 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
923                 wVal |= ALI_SPDIF_OUT_SEL_PCM;
924                 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
925                 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
926         }
927 }
928
929 static void snd_ali_enable_spdif_chnout(ali_t *codec)
930 {
931         unsigned short wVal = 0;
932
933         wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
934         wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
935         outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
936 /*
937         wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
938         if (flag & ALI_SPDIF_OUT_NON_PCM)
939                 wVal |= 0x0002;
940         else    
941                 wVal &= (~0x0002);
942         outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
943 */
944         snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
945 }
946
947 static void snd_ali_disable_spdif_chnout(ali_t *codec)
948 {
949         unsigned short wVal = 0;
950         wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
951         wVal |= ALI_SPDIF_OUT_SEL_PCM;
952         outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
953
954         snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
955 }
956
957 static void snd_ali_disable_spdif_out(ali_t *codec)
958 {
959         unsigned char  bVal;
960
961         bVal = inb(ALI_REG(codec, ALI_SCTRL));
962         outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
963
964         snd_ali_disable_spdif_chnout(codec);
965 }
966
967 static void snd_ali_update_ptr(ali_t *codec,int channel)
968 {
969         snd_ali_voice_t *pvoice = NULL;
970         snd_pcm_runtime_t *runtime;
971         snd_ali_channel_control_t *pchregs = NULL;
972         unsigned int old, mask;
973 #ifdef ALI_DEBUG
974         unsigned int temp, cspf;
975 #endif
976
977         pchregs = &(codec->chregs);
978
979         // check if interrupt occurred for channel
980         old  = pchregs->data.aint;
981         mask = ((unsigned int) 1L) << (channel & 0x1f);
982
983         if (!(old & mask))
984                 return;
985
986         pvoice = &codec->synth.voices[channel];
987         runtime = pvoice->substream->runtime;
988
989         udelay(100);
990         spin_lock(&codec->reg_lock);
991
992         if (pvoice->pcm && pvoice->substream) {
993                 /* pcm interrupt */
994 #ifdef ALI_DEBUG
995                 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
996                 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
997                 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
998 #endif
999                 if (pvoice->running) {
1000                         snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1001                         spin_unlock(&codec->reg_lock);
1002                         snd_pcm_period_elapsed(pvoice->substream);
1003                         spin_lock(&codec->reg_lock);
1004                 } else {
1005                         snd_ali_stop_voice(codec, channel);
1006                         snd_ali_disable_voice_irq(codec, channel);
1007                 }       
1008         } else if (codec->synth.voices[channel].synth) {
1009                 /* synth interrupt */
1010         } else if (codec->synth.voices[channel].midi) {
1011                 /* midi interrupt */
1012         } else {
1013                 /* unknown interrupt */
1014                 snd_ali_stop_voice(codec, channel);
1015                 snd_ali_disable_voice_irq(codec, channel);
1016         }
1017         spin_unlock(&codec->reg_lock);
1018         outl(mask,ALI_REG(codec,pchregs->regs.aint));
1019         pchregs->data.aint = old & (~mask);
1020 }
1021
1022 static void snd_ali_interrupt(ali_t * codec)
1023 {
1024         int channel;
1025         unsigned int audio_int;
1026         snd_ali_channel_control_t *pchregs = NULL;
1027         pchregs = &(codec->chregs);
1028
1029         audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1030         if (audio_int & ADDRESS_IRQ) {
1031                 // get interrupt status for all channels
1032                 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1033                 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1034                         snd_ali_update_ptr(codec, channel);
1035                 }
1036         }
1037         outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1038                 ALI_REG(codec,ALI_MISCINT));
1039 }
1040
1041
1042 static irqreturn_t snd_ali_card_interrupt(int irq,
1043                                    void *dev_id,
1044                                    struct pt_regs *regs)
1045 {
1046         ali_t   *codec = dev_id;
1047
1048         if (codec == NULL)
1049                 return IRQ_NONE;
1050         snd_ali_interrupt(codec);
1051         return IRQ_HANDLED;
1052 }
1053
1054
1055 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1056 {
1057         snd_ali_voice_t *pvoice = NULL;
1058         unsigned long flags;
1059         int idx;
1060
1061         snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1062
1063         spin_lock_irqsave(&codec->voice_alloc, flags);
1064         if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1065                 idx = snd_ali_find_free_channel(codec,rec);
1066                 if(idx < 0) {
1067                         snd_printk("ali_alloc_voice: err.\n");
1068                         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1069                         return NULL;
1070                 }
1071                 pvoice = &(codec->synth.voices[idx]);
1072                 pvoice->use = 1;
1073                 pvoice->pcm = 1;
1074                 pvoice->mode = rec;
1075                 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1076                 return pvoice;
1077         }
1078         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1079         return NULL;
1080 }
1081
1082
1083 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1084 {
1085         unsigned long flags;
1086         void (*private_free)(void *);
1087         void *private_data;
1088
1089         snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1090         if (pvoice == NULL || !pvoice->use)
1091                 return;
1092         snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1093         spin_lock_irqsave(&codec->voice_alloc, flags);
1094         private_free = pvoice->private_free;
1095         private_data = pvoice->private_data;
1096         pvoice->private_free = NULL;
1097         pvoice->private_data = NULL;
1098         if (pvoice->pcm) {
1099                 snd_ali_free_channel_pcm(codec, pvoice->number);
1100         }
1101         pvoice->use = pvoice->pcm = pvoice->synth = 0;
1102         pvoice->substream = NULL;
1103         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1104         if (private_free)
1105                 private_free(private_data);
1106 }
1107
1108
1109 static void snd_ali_clear_voices(ali_t * codec,
1110                           unsigned int v_min,
1111                           unsigned int v_max)
1112 {
1113         unsigned int i;
1114
1115         for (i = v_min; i <= v_max; i++) {
1116                 snd_ali_stop_voice(codec, i);
1117                 snd_ali_disable_voice_irq(codec, i);
1118         }
1119 }
1120
1121 static void snd_ali_write_voice_regs(ali_t * codec,
1122                          unsigned int Channel,
1123                          unsigned int LBA,
1124                          unsigned int CSO,
1125                          unsigned int ESO,
1126                          unsigned int DELTA,
1127                          unsigned int ALPHA_FMS,
1128                          unsigned int GVSEL,
1129                          unsigned int PAN,
1130                          unsigned int VOL,
1131                          unsigned int CTRL,
1132                          unsigned int EC)
1133 {
1134         unsigned int ctlcmds[4];
1135         
1136         outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1137
1138         ctlcmds[0] =  (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1139         ctlcmds[1] =  LBA;
1140         ctlcmds[2] =  (ESO << 16) | (DELTA & 0x0ffff);
1141         ctlcmds[3] =  (GVSEL << 31) |
1142                       ((PAN & 0x0000007f) << 24) |
1143                       ((VOL & 0x000000ff) << 16) |
1144                       ((CTRL & 0x0000000f) << 12) |
1145                       (EC & 0x00000fff);
1146
1147         outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1148
1149         outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1150         outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1151         outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1152         outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1153
1154         outl(0x30000000, ALI_REG(codec, ALI_EBUF1));    /* Still Mode */
1155         outl(0x30000000, ALI_REG(codec, ALI_EBUF2));    /* Still Mode */
1156 }
1157
1158 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1159 {
1160         unsigned int delta;
1161
1162         if (rate < 4000)  rate = 4000;
1163         if (rate > 48000) rate = 48000;
1164
1165         if (rec) {
1166                 if (rate == 44100)
1167                         delta = 0x116a;
1168                 else if (rate == 8000)
1169                         delta = 0x6000;
1170                 else if (rate == 48000)
1171                         delta = 0x1000;
1172                 else
1173                         delta = ((48000 << 12) / rate) & 0x0000ffff;
1174         } else {
1175                 if (rate == 44100)
1176                         delta = 0xeb3;
1177                 else if (rate == 8000)
1178                         delta = 0x2ab;
1179                 else if (rate == 48000)
1180                         delta = 0x1000;
1181                 else 
1182                         delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1183         }
1184
1185         return delta;
1186 }
1187
1188 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1189 {
1190         unsigned int CTRL;
1191         snd_pcm_runtime_t *runtime = substream->runtime;
1192
1193         /* set ctrl mode
1194            CTRL default: 8-bit (unsigned) mono, loop mode enabled
1195          */
1196         CTRL = 0x00000001;
1197         if (snd_pcm_format_width(runtime->format) == 16)
1198                 CTRL |= 0x00000008;     // 16-bit data
1199         if (!snd_pcm_format_unsigned(runtime->format))
1200                 CTRL |= 0x00000002;     // signed data
1201         if (runtime->channels > 1)
1202                 CTRL |= 0x00000004;     // stereo data
1203         return CTRL;
1204 }
1205
1206 /*
1207  *  PCM part
1208  */
1209
1210 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1211                                   unsigned int cmd, void *arg)
1212 {
1213         return snd_pcm_lib_ioctl(substream, cmd, arg);
1214 }
1215
1216 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1217                                int cmd)
1218                                     
1219 {
1220         ali_t *codec = snd_pcm_substream_chip(substream);
1221         struct list_head *pos;
1222         snd_pcm_substream_t *s;
1223         unsigned int what, whati, capture_flag;
1224         snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1225         unsigned int val;
1226         int do_start;
1227
1228         switch (cmd) {
1229         case SNDRV_PCM_TRIGGER_START:
1230         case SNDRV_PCM_TRIGGER_RESUME:
1231                 do_start = 1; break;
1232         case SNDRV_PCM_TRIGGER_STOP:
1233         case SNDRV_PCM_TRIGGER_SUSPEND:
1234                 do_start = 0; break;
1235         default:
1236                 return -EINVAL;
1237         }
1238
1239         what = whati = capture_flag = 0;
1240         snd_pcm_group_for_each(pos, substream) {
1241                 s = snd_pcm_group_substream_entry(pos);
1242                 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1243                         pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1244                         evoice = pvoice->extra;
1245                         what |= 1 << (pvoice->number & 0x1f);
1246                         if (evoice == NULL) {
1247                                 whati |= 1 << (pvoice->number & 0x1f);
1248                         } else {
1249                                 whati |= 1 << (evoice->number & 0x1f);
1250                                 what |= 1 << (evoice->number & 0x1f);
1251                         }
1252                         if (do_start) {
1253                                 pvoice->running = 1;
1254                                 if (evoice != NULL)
1255                                         evoice->running = 1;
1256                         } else {
1257                                 pvoice->running = 0;
1258                                 if (evoice != NULL)
1259                                         evoice->running = 0;
1260                         }
1261                         snd_pcm_trigger_done(s, substream);
1262                         if (pvoice->mode)
1263                                 capture_flag = 1;
1264                 }
1265         }
1266         spin_lock(&codec->reg_lock);
1267         if (! do_start) {
1268                 outl(what, ALI_REG(codec, ALI_STOP));
1269         }
1270         val = inl(ALI_REG(codec, ALI_AINTEN));
1271         if (do_start) {
1272                 val |= whati;
1273         } else {
1274                 val &= ~whati;
1275         }
1276         outl(val, ALI_REG(codec, ALI_AINTEN));
1277         if (do_start) {
1278                 outl(what, ALI_REG(codec, ALI_START));
1279         }
1280         snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1281         spin_unlock(&codec->reg_lock);
1282
1283         return 0;
1284 }
1285
1286 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1287                                  snd_pcm_hw_params_t * hw_params)
1288 {
1289         ali_t *codec = snd_pcm_substream_chip(substream);
1290         snd_pcm_runtime_t *runtime = substream->runtime;
1291         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1292         snd_ali_voice_t *evoice = pvoice->extra;
1293         int err;
1294         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1295         if (err < 0) return err;
1296         
1297         /* voice management */
1298
1299         if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1300                 if (evoice == NULL) {
1301                         evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1302                         if (evoice == NULL)
1303                                 return -ENOMEM;
1304                         pvoice->extra = evoice;
1305                         evoice->substream = substream;
1306                 }
1307         } else {
1308                 if (evoice != NULL) {
1309                         snd_ali_free_voice(codec, evoice);
1310                         pvoice->extra = evoice = NULL;
1311                 }
1312         }
1313
1314         return 0;
1315 }
1316
1317 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1318 {
1319         ali_t *codec = snd_pcm_substream_chip(substream);
1320         snd_pcm_runtime_t *runtime = substream->runtime;
1321         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1322         snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1323
1324         snd_pcm_lib_free_pages(substream);
1325         if (evoice != NULL) {
1326                 snd_ali_free_voice(codec, evoice);
1327                 pvoice->extra = NULL;
1328         }
1329         return 0;
1330 }
1331
1332 static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1333                                  snd_pcm_hw_params_t * hw_params)
1334 {
1335         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1336 }
1337
1338 static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1339 {
1340         return snd_pcm_lib_free_pages(substream);
1341 }
1342
1343 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1344 {
1345         ali_t *codec = snd_pcm_substream_chip(substream);
1346         snd_pcm_runtime_t *runtime = substream->runtime;
1347         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1348         snd_ali_voice_t *evoice = pvoice->extra;
1349         unsigned long flags;
1350
1351         unsigned int LBA;
1352         unsigned int Delta;
1353         unsigned int ESO;
1354         unsigned int CTRL;
1355         unsigned int GVSEL;
1356         unsigned int PAN;
1357         unsigned int VOL;
1358         unsigned int EC;
1359         
1360         snd_ali_printk("playback_prepare ...\n");
1361
1362         spin_lock_irqsave(&codec->reg_lock, flags);     
1363         
1364         /* set Delta (rate) value */
1365         Delta = snd_ali_convert_rate(runtime->rate, 0);
1366
1367         if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) || 
1368             (pvoice->number == ALI_PCM_IN_CHANNEL))
1369                 snd_ali_disable_special_channel(codec, pvoice->number);
1370         else if (codec->spdif_support &&
1371                  (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1372                  && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1373                 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1374                 Delta = 0x1000;
1375         }
1376         
1377         /* set Loop Back Address */
1378         LBA = runtime->dma_addr;
1379
1380         /* set interrupt count size */
1381         pvoice->count = runtime->period_size;
1382
1383         /* set target ESO for channel */
1384         pvoice->eso = runtime->buffer_size; 
1385
1386         snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1387
1388         /* set ESO to capture first MIDLP interrupt */
1389         ESO = pvoice->eso -1;
1390         /* set ctrl mode */
1391         CTRL = snd_ali_control_mode(substream);
1392
1393         GVSEL = 1;
1394         PAN = 0;
1395         VOL = 0;
1396         EC = 0;
1397         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);
1398         snd_ali_write_voice_regs(    codec,
1399                                      pvoice->number,
1400                                      LBA,
1401                                      0, /* cso */
1402                                      ESO,
1403                                      Delta,
1404                                      0, /* alpha */
1405                                      GVSEL,
1406                                      PAN,
1407                                      VOL,
1408                                      CTRL,
1409                                      EC);
1410         if (evoice != NULL) {
1411                 evoice->count = pvoice->count;
1412                 evoice->eso = pvoice->count << 1;
1413                 ESO = evoice->eso - 1;
1414                 snd_ali_write_voice_regs(codec,
1415                                      evoice->number,
1416                                      LBA,
1417                                      0, /* cso */
1418                                      ESO,
1419                                      Delta,
1420                                      0, /* alpha */
1421                                      GVSEL,
1422                                      (unsigned int)0x7f,
1423                                      (unsigned int)0x3ff,
1424                                      CTRL,
1425                                      EC);
1426         }
1427         spin_unlock_irqrestore(&codec->reg_lock, flags);
1428         return 0;
1429 }
1430
1431
1432 static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1433 {
1434         ali_t *codec = snd_pcm_substream_chip(substream);
1435         snd_pcm_runtime_t *runtime = substream->runtime;
1436         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1437         unsigned long flags;
1438         unsigned int LBA;
1439         unsigned int Delta;
1440         unsigned int ESO;
1441         unsigned int CTRL;
1442         unsigned int GVSEL;
1443         unsigned int PAN;
1444         unsigned int VOL;
1445         unsigned int EC;
1446         u8       bValue;
1447
1448         spin_lock_irqsave(&codec->reg_lock, flags);
1449
1450         snd_ali_printk("capture_prepare...\n");
1451
1452         snd_ali_enable_special_channel(codec,pvoice->number);
1453
1454         Delta = snd_ali_convert_rate(runtime->rate, 1);
1455
1456         // Prepare capture intr channel
1457         if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1458
1459                 unsigned int rate;
1460                 
1461                 if (codec->revision != ALI_5451_V02) {
1462                         spin_unlock_irqrestore(&codec->reg_lock, flags);                        
1463                         return -1;
1464                 }
1465                 rate = snd_ali_get_spdif_in_rate(codec);
1466                 if (rate == 0) {
1467                         snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1468                         rate = 48000;
1469                 }
1470                 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1471                 if (bValue & 0x10) {
1472                         outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1473                         printk("clear SPDIF parity error flag.\n");
1474                 }
1475
1476                 if (rate != 48000)
1477                         Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1478         }
1479
1480         // set target ESO for channel 
1481         pvoice->eso = runtime->buffer_size; 
1482
1483         // set interrupt count size 
1484         pvoice->count = runtime->period_size;
1485
1486         // set Loop Back Address 
1487         LBA = runtime->dma_addr;
1488
1489         // set ESO to capture first MIDLP interrupt 
1490         ESO = pvoice->eso - 1;
1491         CTRL = snd_ali_control_mode(substream);
1492         GVSEL = 0;
1493         PAN = 0x00;
1494         VOL = 0x00;
1495         EC = 0;
1496
1497         snd_ali_write_voice_regs(    codec,
1498                                      pvoice->number,
1499                                      LBA,
1500                                      0, /* cso */
1501                                      ESO,
1502                                      Delta,
1503                                      0, /* alpha */
1504                                      GVSEL,
1505                                      PAN,
1506                                      VOL,
1507                                      CTRL,
1508                                      EC);
1509
1510
1511         spin_unlock_irqrestore(&codec->reg_lock, flags);
1512
1513         return 0;
1514 }
1515
1516
1517 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1518 {
1519         ali_t *codec = snd_pcm_substream_chip(substream);
1520         snd_pcm_runtime_t *runtime = substream->runtime;
1521         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1522         unsigned int cso;
1523
1524         spin_lock(&codec->reg_lock);
1525         if (!pvoice->running) {
1526                 spin_unlock(&codec->reg_lock);
1527                 return 0;
1528         }
1529         outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1530         cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1531         spin_unlock(&codec->reg_lock);
1532         snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1533
1534         return cso;
1535 }
1536
1537
1538 static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1539 {
1540         ali_t *codec = snd_pcm_substream_chip(substream);
1541         snd_pcm_runtime_t *runtime = substream->runtime;
1542         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1543         unsigned int cso;
1544         unsigned long flags;
1545
1546         spin_lock_irqsave(&codec->reg_lock, flags);
1547         if (!pvoice->running) {
1548                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1549                 return 0;
1550         }
1551         outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1552         cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1553         spin_unlock_irqrestore(&codec->reg_lock, flags);
1554
1555         return cso;
1556 }
1557
1558 static snd_pcm_hardware_t snd_ali_playback =
1559 {
1560         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1561                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1562                                  SNDRV_PCM_INFO_MMAP_VALID |
1563                                  SNDRV_PCM_INFO_RESUME |
1564                                  SNDRV_PCM_INFO_SYNC_START),
1565         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1566                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1567         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1568         .rate_min =             4000,
1569         .rate_max =             48000,
1570         .channels_min =         1,
1571         .channels_max =         2,
1572         .buffer_bytes_max =     (256*1024),
1573         .period_bytes_min =     64,
1574         .period_bytes_max =     (256*1024),
1575         .periods_min =          1,
1576         .periods_max =          1024,
1577         .fifo_size =            0,
1578 };
1579
1580 /*
1581  *  Capture support device description
1582  */
1583
1584 static snd_pcm_hardware_t snd_ali_capture =
1585 {
1586         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1587                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1588                                  SNDRV_PCM_INFO_MMAP_VALID |
1589                                  SNDRV_PCM_INFO_RESUME |
1590                                  SNDRV_PCM_INFO_SYNC_START),
1591         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1592                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1593         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1594         .rate_min =             4000,
1595         .rate_max =             48000,
1596         .channels_min =         1,
1597         .channels_max =         2,
1598         .buffer_bytes_max =     (128*1024),
1599         .period_bytes_min =     64,
1600         .period_bytes_max =     (128*1024),
1601         .periods_min =          1,
1602         .periods_max =          1024,
1603         .fifo_size =            0,
1604 };
1605
1606 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1607 {
1608         unsigned long flags;
1609         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1610         ali_t *codec;
1611
1612         if (pvoice) {
1613                 codec = pvoice->codec;
1614                 spin_lock_irqsave(&codec->reg_lock, flags);
1615                 snd_ali_free_voice(pvoice->codec, pvoice);
1616                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1617         }
1618 }
1619
1620 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1621 {
1622         ali_t *codec = snd_pcm_substream_chip(substream);
1623         snd_pcm_runtime_t *runtime = substream->runtime;
1624         snd_ali_voice_t *pvoice;
1625         unsigned long flags = 0;
1626
1627         spin_lock_irqsave(&codec->reg_lock, flags);
1628         pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1629         if (pvoice == NULL) {
1630                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1631                 return -EAGAIN;
1632         }
1633         pvoice->codec = codec;
1634         spin_unlock_irqrestore(&codec->reg_lock, flags);
1635
1636         pvoice->substream = substream;
1637         runtime->private_data = pvoice;
1638         runtime->private_free = snd_ali_pcm_free_substream;
1639
1640         runtime->hw = snd_ali_playback;
1641         snd_pcm_set_sync(substream);
1642         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1643         return 0;
1644 }
1645
1646
1647 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1648 {
1649         ali_t *codec = snd_pcm_substream_chip(substream);
1650         snd_pcm_runtime_t *runtime = substream->runtime;
1651         snd_ali_voice_t *pvoice;
1652         unsigned long flags;
1653
1654         spin_lock_irqsave(&codec->reg_lock, flags);
1655         pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1656         if (pvoice == NULL) {
1657                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1658                 return -EAGAIN;
1659         }
1660         pvoice->codec = codec;
1661         spin_unlock_irqrestore(&codec->reg_lock, flags);
1662
1663         pvoice->substream = substream;
1664         runtime->private_data = pvoice;
1665         runtime->private_free = snd_ali_pcm_free_substream;
1666         runtime->hw = snd_ali_capture;
1667         snd_pcm_set_sync(substream);
1668         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1669         return 0;
1670 }
1671
1672
1673 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1674 {
1675         return 0;
1676 }
1677
1678 static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1679 {
1680         ali_t *codec = snd_pcm_substream_chip(substream);
1681         snd_pcm_runtime_t *runtime = substream->runtime;
1682         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1683
1684         snd_ali_disable_special_channel(codec,pvoice->number);
1685
1686         return 0;
1687 }
1688
1689 static snd_pcm_ops_t snd_ali_playback_ops = {
1690         .open =         snd_ali_playback_open,
1691         .close =        snd_ali_playback_close,
1692         .ioctl =        snd_ali_ioctl,
1693         .hw_params =    snd_ali_playback_hw_params,
1694         .hw_free =      snd_ali_playback_hw_free,
1695         .prepare =      snd_ali_playback_prepare,
1696         .trigger =      snd_ali_trigger,
1697         .pointer =      snd_ali_playback_pointer,
1698 };
1699
1700 static snd_pcm_ops_t snd_ali_capture_ops = {
1701         .open =         snd_ali_capture_open,
1702         .close =        snd_ali_capture_close,
1703         .ioctl =        snd_ali_ioctl,
1704         .hw_params =    snd_ali_capture_hw_params,
1705         .hw_free =      snd_ali_capture_hw_free,
1706         .prepare =      snd_ali_capture_prepare,
1707         .trigger =      snd_ali_trigger,
1708         .pointer =      snd_ali_capture_pointer,
1709 };
1710
1711
1712 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1713 {
1714         ali_t *codec = pcm->private_data;
1715         codec->pcm = NULL;
1716 }
1717
1718 static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1719 {
1720         snd_pcm_t *pcm;
1721         int err;
1722
1723         if (rpcm) *rpcm = NULL;
1724         err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1725         if (err < 0) {
1726                 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1727                 return err;
1728         }
1729         pcm->private_data = codec;
1730         pcm->private_free = snd_ali_pcm_free;
1731         pcm->info_flags = 0;
1732         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1733         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1734
1735         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1736                                               snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1737
1738         pcm->info_flags = 0;
1739         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1740         strcpy(pcm->name, "ALI 5451");
1741         codec->pcm = pcm;
1742         if (rpcm) *rpcm = pcm;
1743         return 0;
1744 }
1745
1746 #define ALI5451_SPDIF(xname, xindex, value) \
1747 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1748 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1749 .put = snd_ali5451_spdif_put, .private_value = value}
1750
1751 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1752 {
1753         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1754         uinfo->count = 1;
1755         uinfo->value.integer.min = 0;
1756         uinfo->value.integer.max = 1;
1757         return 0;
1758 }
1759
1760 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1761 {
1762         unsigned long flags;
1763         ali_t *codec = kcontrol->private_data;
1764         unsigned int enable;
1765
1766         enable = ucontrol->value.integer.value[0] ? 1 : 0;
1767
1768         spin_lock_irqsave(&codec->reg_lock, flags);
1769         switch(kcontrol->private_value) {
1770         case 0:
1771                 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1772                 break;
1773         case 1:
1774                 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1775                 break;
1776         case 2:
1777                 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1778                 break;
1779         default:
1780                 break;
1781         }
1782         ucontrol->value.integer.value[0] = enable;
1783         spin_unlock_irqrestore(&codec->reg_lock, flags);
1784         return 0;
1785 }
1786
1787 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1788 {
1789         unsigned long flags;
1790         ali_t *codec = kcontrol->private_data;
1791         unsigned int change = 0, enable = 0;
1792
1793         enable = ucontrol->value.integer.value[0] ? 1 : 0;
1794
1795         spin_lock_irqsave(&codec->reg_lock, flags);
1796         switch (kcontrol->private_value) {
1797         case 0:
1798                 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1799                 change = change ^ enable;
1800                 if (change) {
1801                         if (enable) {
1802                                 codec->spdif_mask |= 0x02;
1803                                 snd_ali_enable_spdif_out(codec);
1804                         } else {
1805                                 codec->spdif_mask &= ~(0x02);
1806                                 codec->spdif_mask &= ~(0x04);
1807                                 snd_ali_disable_spdif_out(codec);
1808                         }
1809                 }
1810                 break;
1811         case 1: 
1812                 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1813                 change = change ^ enable;
1814                 if (change && (codec->spdif_mask & 0x02)) {
1815                         if (enable) {
1816                                 codec->spdif_mask |= 0x04;
1817                                 snd_ali_enable_spdif_chnout(codec);
1818                         } else {
1819                                 codec->spdif_mask &= ~(0x04);
1820                                 snd_ali_disable_spdif_chnout(codec);
1821                         }
1822                 }
1823                 break;
1824         case 2:
1825                 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1826                 change = change ^ enable;
1827                 if (change) {
1828                         if (enable) {
1829                                 codec->spdif_mask |= 0x01;
1830                                 snd_ali_enable_spdif_in(codec);
1831                         } else {
1832                                 codec->spdif_mask &= ~(0x01);
1833                                 snd_ali_disable_spdif_in(codec);
1834                         }
1835                 }
1836                 break;
1837         default:
1838                 break;
1839         }
1840         spin_unlock_irqrestore(&codec->reg_lock, flags);
1841         
1842         return change;
1843 }
1844
1845 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1846         /* spdif aplayback switch */
1847         /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1848         ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1849         /* spdif out to spdif channel */
1850         ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1851         /* spdif in from spdif channel */
1852         ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1853 };
1854
1855 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1856 {
1857         ali_t *codec = bus->private_data;
1858         codec->ac97_bus = NULL;
1859 }
1860
1861 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1862 {
1863         ali_t *codec = ac97->private_data;
1864         codec->ac97 = NULL;
1865 }
1866
1867 static int __devinit snd_ali_mixer(ali_t * codec)
1868 {
1869         ac97_template_t ac97;
1870         unsigned int idx;
1871         int err;
1872         static ac97_bus_ops_t ops = {
1873                 .write = snd_ali_codec_write,
1874                 .read = snd_ali_codec_read,
1875         };
1876
1877         if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1878                 return err;
1879         codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1880
1881         memset(&ac97, 0, sizeof(ac97));
1882         ac97.private_data = codec;
1883         ac97.private_free = snd_ali_mixer_free_ac97;
1884         if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1885                 snd_printk("ali mixer creating error.\n");
1886                 return err;
1887         }
1888         if (codec->spdif_support) {
1889                 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1890                         err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1891                         if (err < 0) return err;
1892                 }
1893         }
1894         return 0;
1895 }
1896
1897 #ifdef CONFIG_PM
1898 static int ali_suspend(snd_card_t *card, unsigned int state)
1899 {
1900         ali_t *chip = card->pm_private_data;
1901         ali_image_t *im;
1902         int i, j;
1903
1904         im = chip->image;
1905         if (! im)
1906                 return 0;
1907
1908         snd_pcm_suspend_all(chip->pcm);
1909         snd_ac97_suspend(chip->ac97);
1910
1911         spin_lock_irq(&chip->reg_lock);
1912         
1913         im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1914         // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1915         im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1916         
1917         // disable all IRQ bits
1918         outl(0, ALI_REG(chip, ALI_MISCINT));
1919         
1920         for (i = 0; i < ALI_GLOBAL_REGS; i++) { 
1921                 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1922                         continue;
1923                 im->regs[i] = inl(ALI_REG(chip, i*4));
1924         }
1925         
1926         for (i = 0; i < ALI_CHANNELS; i++) {
1927                 outb(i, ALI_REG(chip, ALI_GC_CIR));
1928                 for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1929                         im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1930         }
1931
1932         // stop all HW channel
1933         outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1934
1935         spin_unlock_irq(&chip->reg_lock);
1936         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1937         return 0;
1938 }
1939
1940 static int ali_resume(snd_card_t *card, unsigned int state)
1941 {
1942         ali_t *chip = card->pm_private_data;
1943         ali_image_t *im;
1944         int i, j;
1945
1946         im = chip->image;
1947         if (! im)
1948                 return 0;
1949
1950         pci_enable_device(chip->pci);
1951
1952         spin_lock_irq(&chip->reg_lock);
1953         
1954         for (i = 0; i < ALI_CHANNELS; i++) {
1955                 outb(i, ALI_REG(chip, ALI_GC_CIR));
1956                 for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1957                         outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1958         }
1959         
1960         for (i = 0; i < ALI_GLOBAL_REGS; i++) { 
1961                 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
1962                         continue;
1963                 outl(im->regs[i], ALI_REG(chip, i*4));
1964         }
1965         
1966         // start HW channel
1967         outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1968         // restore IRQ enable bits
1969         outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1970         
1971         spin_unlock_irq(&chip->reg_lock);
1972
1973         snd_ac97_resume(chip->ac97);
1974         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1975         
1976         return 0;
1977 }
1978 #endif /* CONFIG_PM */
1979
1980 static int snd_ali_free(ali_t * codec)
1981 {
1982         if (codec->hw_initialized)
1983                 snd_ali_disable_address_interrupt(codec);
1984         if (codec->irq >= 0) {
1985                 synchronize_irq(codec->irq);
1986                 free_irq(codec->irq, (void *)codec);
1987         }
1988         if (codec->port)
1989                 pci_release_regions(codec->pci);
1990 #ifdef CONFIG_PM
1991         if (codec->image)
1992                 kfree(codec->image);
1993 #endif
1994         kfree(codec);
1995         return 0;
1996 }
1997
1998 static int snd_ali_chip_init(ali_t *codec)
1999 {
2000         unsigned int legacy;
2001         unsigned char temp;
2002         struct pci_dev *pci_dev = NULL;
2003
2004         snd_ali_printk("chip initializing ... \n");
2005
2006         if (snd_ali_reset_5451(codec)) {
2007                 snd_printk("ali_chip_init: reset 5451 error.\n");
2008                 return -1;
2009         }
2010
2011         if (codec->revision == ALI_5451_V02) {
2012                 pci_dev = codec->pci_m1533;
2013                 pci_read_config_byte(pci_dev, 0x59, &temp);
2014                 temp |= 0x80;
2015                 pci_write_config_byte(pci_dev, 0x59, temp);
2016         
2017                 pci_dev = codec->pci_m7101;
2018                 pci_read_config_byte(pci_dev, 0xb8, &temp);
2019                 temp |= 0x20;
2020                 pci_write_config_byte(pci_dev, 0xB8, temp);
2021         }
2022
2023         pci_read_config_dword(codec->pci, 0x44, &legacy);
2024         legacy &= 0xff00ff00;
2025         legacy |= 0x000800aa;
2026         pci_write_config_dword(codec->pci, 0x44, legacy);
2027
2028         outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2029         outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2030         outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2031         outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2032         outb(0x10,       ALI_REG(codec, ALI_MPUR2));
2033
2034         codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2035         codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2036         if (codec->spdif_support) {
2037                 snd_ali_enable_spdif_out(codec);
2038                 codec->spdif_mask = 0x00000002;
2039         }
2040
2041         snd_ali_printk("chip initialize succeed.\n");
2042         return 0;
2043
2044 }
2045
2046 static int __devinit snd_ali_resources(ali_t *codec)
2047 {
2048         int err;
2049
2050         snd_ali_printk("resouces allocation ...\n");
2051         if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2052                 return err;
2053         codec->port = pci_resource_start(codec->pci, 0);
2054
2055         if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2056                 snd_printk("Unable to request irq.\n");
2057                 return -EBUSY;
2058         }
2059         codec->irq = codec->pci->irq;
2060         snd_ali_printk("resouces allocated.\n");
2061         return 0;
2062 }
2063 static int snd_ali_dev_free(snd_device_t *device) 
2064 {
2065         ali_t *codec=device->device_data;
2066         snd_ali_free(codec);
2067         return 0;
2068 }
2069
2070 static int __devinit snd_ali_create(snd_card_t * card,
2071                                     struct pci_dev *pci,
2072                                     int pcm_streams,
2073                                     int spdif_support,
2074                                     ali_t ** r_ali)
2075 {
2076         ali_t *codec;
2077         int i, err;
2078         unsigned short cmdw = 0;
2079         struct pci_dev *pci_dev = NULL;
2080         static snd_device_ops_t ops = {
2081                 (snd_dev_free_t *)snd_ali_dev_free,
2082                 NULL,
2083                 NULL
2084         };
2085
2086         *r_ali = NULL;
2087
2088         snd_ali_printk("creating ...\n");
2089
2090         /* enable PCI device */
2091         if ((err = pci_enable_device(pci)) < 0)
2092                 return err;
2093         /* check, if we can restrict PCI DMA transfers to 31 bits */
2094         if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2095             pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2096                 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2097                 return -ENXIO;
2098         }
2099
2100         if ((codec = kcalloc(1, sizeof(*codec), GFP_KERNEL)) == NULL)
2101                 return -ENOMEM;
2102
2103         spin_lock_init(&codec->reg_lock);
2104         spin_lock_init(&codec->voice_alloc);
2105
2106         codec->card = card;
2107         codec->pci = pci;
2108         codec->irq = -1;
2109         pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2110         codec->spdif_support = spdif_support;
2111
2112         if (pcm_streams < 1)
2113                 pcm_streams = 1;
2114         if (pcm_streams > 32)
2115                 pcm_streams = 32;
2116         
2117         pci_set_master(pci);
2118         pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2119         if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2120                 cmdw |= PCI_COMMAND_IO;
2121                 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2122         }
2123         pci_set_master(pci);
2124         
2125         if (snd_ali_resources(codec)) {
2126                 snd_ali_free(codec);
2127                 return -EBUSY;
2128         }
2129
2130         synchronize_irq(pci->irq);
2131
2132         codec->synth.chmap = 0;
2133         codec->synth.chcnt = 0;
2134         codec->spdif_mask = 0;
2135         codec->synth.synthcount = 0;
2136
2137         if (codec->revision == ALI_5451_V02)
2138                 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2139         else
2140                 codec->chregs.regs.ac97read = ALI_AC97_READ;
2141         codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2142
2143         codec->chregs.regs.start  = ALI_START;
2144         codec->chregs.regs.stop   = ALI_STOP;
2145         codec->chregs.regs.aint   = ALI_AINT;
2146         codec->chregs.regs.ainten = ALI_AINTEN;
2147
2148         codec->chregs.data.start  = 0x00;
2149         codec->chregs.data.stop   = 0x00;
2150         codec->chregs.data.aint   = 0x00;
2151         codec->chregs.data.ainten = 0x00;
2152
2153         /* M1533: southbridge */
2154         pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2155         codec->pci_m1533 = pci_dev;
2156         if (! codec->pci_m1533) {
2157                 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2158                 snd_ali_free(codec);
2159                 return -ENODEV;
2160         }
2161         /* M7101: power management */
2162         pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2163         codec->pci_m7101 = pci_dev;
2164         if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2165                 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2166                 snd_ali_free(codec);
2167                 return -ENODEV;
2168         }
2169
2170         snd_ali_printk("snd_device_new is called.\n");
2171         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2172                 snd_ali_free(codec);
2173                 return err;
2174         }
2175
2176         /* initialise synth voices*/
2177         for (i = 0; i < ALI_CHANNELS; i++ ) {
2178                 codec->synth.voices[i].number = i;
2179         }
2180
2181         if ((err = snd_ali_chip_init(codec)) < 0) {
2182                 snd_printk("ali create: chip init error.\n");
2183                 return err;
2184         }
2185
2186 #ifdef CONFIG_PM
2187         codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2188         if (! codec->image)
2189                 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2190         else
2191                 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2192 #endif
2193
2194         snd_ali_enable_address_interrupt(codec);
2195         codec->hw_initialized = 1;
2196
2197         *r_ali = codec;
2198         snd_ali_printk("created.\n");
2199         return 0;
2200 }
2201
2202 static int __devinit snd_ali_probe(struct pci_dev *pci,
2203                                    const struct pci_device_id *pci_id)
2204 {
2205         static int dev;
2206         snd_card_t *card;
2207         ali_t *codec;
2208         int err;
2209
2210         snd_ali_printk("probe ...\n");
2211
2212         if (dev >= SNDRV_CARDS)
2213                 return -ENODEV;
2214         if (!enable[dev]) {
2215                 dev++;
2216                 return -ENOENT;
2217         }
2218
2219         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2220         if (card == NULL)
2221                 return -ENOMEM;
2222
2223         if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2224                 snd_card_free(card);
2225                 return err;
2226         }
2227
2228         snd_ali_printk("mixer building ...\n");
2229         if ((err = snd_ali_mixer(codec)) < 0) {
2230                 snd_card_free(card);
2231                 return err;
2232         }
2233         
2234         snd_ali_printk("pcm building ...\n");
2235         if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2236                 snd_card_free(card);
2237                 return err;
2238         }
2239
2240         strcpy(card->driver, "ALI5451");
2241         strcpy(card->shortname, "ALI 5451");
2242         
2243         sprintf(card->longname, "%s at 0x%lx, irq %li",
2244                 card->shortname, codec->port, codec->irq);
2245
2246         snd_ali_printk("register card.\n");
2247         if ((err = snd_card_register(card)) < 0) {
2248                 snd_card_free(card);
2249                 return err;
2250         }
2251         pci_set_drvdata(pci, card);
2252         dev++;
2253         return 0;
2254 }
2255
2256 static void __devexit snd_ali_remove(struct pci_dev *pci)
2257 {
2258         snd_card_free(pci_get_drvdata(pci));
2259         pci_set_drvdata(pci, NULL);
2260 }
2261
2262 static struct pci_driver driver = {
2263         .name = "ALI 5451",
2264         .id_table = snd_ali_ids,
2265         .probe = snd_ali_probe,
2266         .remove = __devexit_p(snd_ali_remove),
2267         SND_PCI_PM_CALLBACKS
2268 };                                
2269
2270 static int __init alsa_card_ali_init(void)
2271 {
2272         return pci_module_init(&driver);
2273 }
2274
2275 static void __exit alsa_card_ali_exit(void)
2276 {
2277         pci_unregister_driver(&driver);
2278 }
2279
2280 module_init(alsa_card_ali_init)
2281 module_exit(alsa_card_ali_exit)